list/classmethod problems
Bruno Desthuilliers
bdesth.quelquechose at free.quelquepart.fr
Mon Mar 13 19:08:24 EST 2006
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
More information about the Python-list
mailing list