I'm pretty new to python and am trying to write a fairly small
application to learn more about the language. I'm noticing some
unexpected behavior in using lists in some classes to hold child
objects. Here is some abbreviated code to help me explain.
############### ############### ######
class Item(object)
__text = ""
def __get_text(self ):
return self.__text
def __set_text(self , value):
self.__text = value
text = property(fget=_ _get_text, fset=__set_text )
def __init__(self, text=""):
self.__text = text
#...some other methods here...
class Parent(object):
__items = []
def __get_items(sel f):
return self.__items
items = property(fget=_ _get_items)
def addItem(self, item):
"""Adds an Item object to the internal list."""
self.__items.ap pend(item)
def __init__(self, items=[]):
if(len(items)>0 ):
for item in items:
self.addItem(it em)
def __str__(self):
s = "<parent>"
for item in self.__items:
s += "<item>%s</item>" % item.text
s += "</parent>"
#...some other methods here...
if(__name__=="_ _main__"):
i1 = Item("one")
i2 = Item("two")
i3 = Item("three")
p1 = Parent([i1, i2])
p2 = Parent([i3])
print str(p1)
############### ############### ######
When I run this script, I expect to see the following string printed:
"<parent><item> one</item><item>two</item></parent>"
Instead, I see the following:
"<parent><item> one</item><item>two</item><item>thre e</item></parent>"
Apparently, the p1 instance somehow thinks that the i3 instance is in
its list. The i3 instance should instead be in the list for p2. By the
way, when I call the __str__() method of p2, I get the same results as
when I do it for p1. The list appears to be acting as if it were a
static member - which it is not.
I do have some @classmethod methods in these classes in my complete
script. Would that confuse the interpreter into thinking that other
members are also static?
I can't seem to find any information on this problem. Does anyone have
any ideas?
Thank you. 6 1668
> Apparently, the p1 instance somehow thinks that the i3 instance is in its list. The i3 instance should instead be in the list for p2. By the way, when I call the __str__() method of p2, I get the same results as when I do it for p1. The list appears to be acting as if it were a static member - which it is not.
I do have some @classmethod methods in these classes in my complete script. Would that confuse the interpreter into thinking that other members are also static?
I can't seem to find any information on this problem. Does anyone have any ideas?
Read this: http://www.python.org/doc/faq/genera...etween-objects
HTH,
Diez
ahart wrote: I'm pretty new to python and am trying to write a fairly small application to learn more about the language. I'm noticing some unexpected behavior in using lists in some classes to hold child objects. Here is some abbreviated code to help me explain.
When I run this script, I expect to see the following string printed:
"<parent><item> one</item><item>two</item></parent>"
Instead, I see the following:
"<parent><item> one</item><item>two</item><item>thre e</item></parent>"
Apparently, the p1 instance somehow thinks that the i3 instance is in its list. The i3 instance should instead be in the list for p2. By the way, when I call the __str__() method of p2, I get the same results as when I do it for p1. The list appears to be acting as if it were a static member - which it is not.
I do have some @classmethod methods in these classes in my complete script. Would that confuse the interpreter into thinking that other members are also static?
I can't seem to find any information on this problem. Does anyone have any ideas?
First, a few kvetches:
(1) Submit the _actual_code_ when you have problems, not paraphrases: class Item(object)
should have been (and probably was):
class Item(object):
def __str__(self): ... s += "</parent>"
should have been (and probably was):
def __str__(self):
...
s += "</parent>"
return s
(2) You must have been a Java or C++ programmer. Don't be so stingy
with access to your variables and methods (all the __xxx methods
and variables). Just use a single underscore (at least for the
methods) telling users, "don't muck with this." The double
underscores make figuring out what's going interactively awkward.
(3) The if statement takes a boolean expression, not parens around a
boolean expression. "if a > b:" is the way to write a test, not
"if(a>b):" (which is why I think Java or C++).
And now to your actual question:
############### ############### ###### class Item(object) __text = ""
The line above is unnecessary (and deceptive). It sets a _class_
variable named _Item__text to a zero-length string.
def __get_text(self ): return self.__text def __set_text(self , value): self.__text = value text = property(fget=_ _get_text, fset=__set_text )
def __init__(self, text=""): self.__text = text
This line sets an _instance_ variable named _Item__text to the arg. #...some other methods here...
class Parent(object): __items = []
The line above is unnecessary (and deceptive). You want
a list-per instance, but you are accessing the class variable below
def __get_items(sel f): return self.__items items = property(fget=_ _get_items)
def addItem(self, item): """Adds an Item object to the internal list.""" self.__items.ap pend(item)
def __init__(self, items=[]): if(len(items)>0 ): for item in items: self.addItem(it em)
If you wrote this method as either:
def __init__(self, items=[]):
self.__items = []
for item in items:
self.addItem(it em)
or:
def __init__(self, items=[]):
self.__items = list(items)
You would have the effect you want -- a list per instance.
By the way, if(len(items)>0 ): for item in items: self.addItem(it em)
Two complaints about this:
First, the "Pythonic way to test for a non-zero length list is "if var"
Second, even if the test were: if items: for item in items: self.addItem(it em)
That code is just plain slower (and less clear) than:
for item in items:
self.addItem(it em)
If items is zero length, it goes through the loop body zero times. def __str__(self): s = "<parent>" for item in self.__items: s += "<item>%s</item>" % item.text s += "</parent>"
#...some other methods here...
if(__name__=="_ _main__"): i1 = Item("one") i2 = Item("two") i3 = Item("three")
p1 = Parent([i1, i2]) p2 = Parent([i3])
print str(p1) ############### ############### ######
Now you get the output you expect.
By-the-by, why shouldn't the first class be simply?:
class Item(object)
def __init__(self, text=""):
self.text = text
Finally, defining __repr__ rather than __str__ will make it
easier to fiddle with your code interactively w/o keeping anything
from working.
--Scott David Daniels sc***********@a cm.org
ahart a écrit : I'm pretty new to python and am trying to write a fairly small application to learn more about the language. I'm noticing some unexpected behavior in using lists in some classes to hold child objects. Here is some abbreviated code to help me explain.
############### ############### ###### class Item(object) __text = "" def __get_text(self ): return self.__text def __set_text(self , value): self.__text = value text = property(fget=_ _get_text, fset=__set_text )
If you don't have any computation to do in the getter/setter, just use
direct attribute access. If the need arise, then it will be time to use
a property.
Also, takes care with leading double underscores, they may not do what
you think. The usual convention is to use a single leading underscore
for implementation details.
(snip)
class Parent(object): __items = []
This is a class attribute. It will be shared by all instances.
(snip)
The list appears to be acting as if it were a static member - which it is not.
Well, in Python it's called a class attribute, but that's pretty much
the same thing.
I do have some @classmethod methods in these classes in my complete script. Would that confuse the interpreter into thinking that other members are also static?
Don't you think such a bug would have been spotted a long time ago ?-)
If you come from Java, take the time to read this: http://dirtsimple.org/2004/12/python-is-not-java.html
HTH
Diez, Scott, and Bruno,
I thank you all for your help and suggestions. I wasn't aware that
default values were considered class (static) values. That seems a
little odd to me, but as long as I know that's the case, I'll be fine.
I initialized my list member in the __init__() method and all is
working as it should now.
Again, I thank you all.
ahart wrote: Diez, Scott, and Bruno,
I thank you all for your help and suggestions. I wasn't aware that default values were considered class (static) values.
These are *not* 'default values'. Defining a name in the body of a class
statement bind that name to the *class*. To bind a name to the instance,
you have to do it inside an instance method - usually in the __init__()
method. Understand that there's not way to bind anything to an instance
before you actually *have* an instance !-)
That seems a little odd to me,
Yes, it's very different from languages like Java or C++, but it makes
perfect sens once you understand Python's object model.
(snip)
Again, I thank you all.
You're welcome.
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom. gro'.split('@')])"
ahart wrote: I thank you all for your help and suggestions. I wasn't aware that default values were considered class (static) values. That seems a little odd to me, but as long as I know that's the case, I'll be fine.
It's all very simple and regular: Things in the class scope
is shared between all instances of the class. This applies to
both __text, __get_text, __set_text, text and __init__ in the
Item class. All these things are shared, since they are defined
in the scope of the class. That's the namespace where they
exists, not the instance. The typical thing to put in the class
scope is obviously methods, although some plain data can belong
there too.
When you access something in the instance, e.g. self.__text,
where self is an Item instance, it will follow a certain lookup
procedure. If it finds __text in the instance, it will use that
object. If not, it will look in the scope of the class, and if
it's not found there, it will look in base classes of Item and
so on.
This means that __get_text will find Item.__text if __text does
not exist in the instance object. On the other hand, when you
assign to self.__text in __set_text, you will get a new name
__text in the instance, and the next __get_item call will find
a __text in the instance, not the shared in the class scope.
Assignments in Python means "make this name refer to that object".
As far as I understand, that's like Java if you don't use those
half-life types like int that aren't proper objects. So, it does
never mean "copy a value to this location in memory" as it does
in C / C++.
With the list in Parent, you never make any assignment, so
there will never be any __item in the instance namespace. All
self.__items lookups will end up in the class namespace.
Since lists are mutable, you can change this list object, and
the changes will be visible to all instance. With the __text
attribute in Item, which is a string (immutable) there is no
way to change the value. To get a new __text value in the
class scope, you'd have to do self.__class__. __text = ... or
Item.__text = ..., and I doubt that you would do that by
accident! ;^)
What made you try to put your private attributes in the class
scope instead of putting them in __init__? Perhaps it's hard
to entirely give up the idea of somehow declaring your class
attributes even if there is no such things as variable
declarations in Python. :) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Karl Chen |
last post by:
Hi.
Is it possible to make a classmethod out of __getitem__?
I.e. I want to do:
class C:
def __getitem__(p):
return 1+p
|
by: Istvan Albert |
last post by:
Paul McGuire wrote:
> Please reconsider the "def f() :" construct. Instead of
> invoking a special punctuation character, it uses context and placement,
> with familiar old 's, to infuse the declaration of a function with special
> characteristics. If this causes def lines to run longer than one line,
> perhaps the same rule that allows an unmatched "(" to carry over multiple
> lines without requiring "\" continuation markers could be...
|
by: C Gillespie |
last post by:
Hi,
Does anyone know of any examples on how (& where) to use staticmethods and
classmethods?
Thanks
Colin
|
by: flyaflya |
last post by:
a = "(1,2,3)"
I want convert a to tuple:(1,2,3),but tuple(a) return ('(', '1', ',',
'2', ',', '3', ')') not (1,2,3)
|
by: Giovanni Bajo |
last post by:
Hello,
what's the magic needed to reuse the base-class implementation of a
classmethod?
class A(object):
@classmethod
def foo(cls, a,b):
# do something
pass
| |
by: Laszlo Zsolt Nagy |
last post by:
Hello,
Is it possible to tell, which instance was used to call the classmethod
that is currently running?
Background: I have a class called DatabaseConnection and it has a
classmethod called process_create_tables. This method should create some
database tables defined by a database definition object. The
DatabaseConnection has many descendants, for example
PostgreSQLConnection. Descendants know how to create tables in a given
|
by: andychambers2002 |
last post by:
Hi,
I'm trying to write a method that needs to know both the class name and
the instance details
class A:
@classmethod
def meth(cls, self):
print cls
|
by: james_027 |
last post by:
hi,
python's staticmethod is the equivalent of java staticmethod right?
with classmethod, I can call the method without the need for creating
an instance right? since the difference between the two is that
classmethod receives the class itself as implicti first argument. From
my understanding classmethod are for dealing with class attributes?
Can somebody teach me the real use of classmethod & staticmethod?
|
by: Matthew Keene |
last post by:
I would like to be able to call a specific classmethod on a class name
that is going to be passed from another parameter. In other words, I
have a call that looks something like:
x = Foo.bar()
and I would like to generalise this so that I can make this call on any
particular class which provides the bar classmethod.
I have implemented this using exec, like so:
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |