473,372 Members | 1,506 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

list/classmethod problems

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(self):
return self.__items
items = property(fget=__get_items)

def addItem(self, item):
"""Adds an Item object to the internal list."""
self.__items.append(item)

def __init__(self, items=[]):
if(len(items)>0):
for item in items:
self.addItem(item)

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>three</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.

Mar 13 '06 #1
6 1612
>
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
Mar 13 '06 #2
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>three</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(self):
return self.__items
items = property(fget=__get_items)

def addItem(self, item):
"""Adds an Item object to the internal list."""
self.__items.append(item)

def __init__(self, items=[]):
if(len(items)>0):
for item in items:
self.addItem(item)
If you wrote this method as either:
def __init__(self, items=[]):
self.__items = []
for item in items:
self.addItem(item)
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(item) 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(item) That code is just plain slower (and less clear) than:
for item in items:
self.addItem(item)
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***********@acm.org
Mar 13 '06 #3
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
Mar 13 '06 #4
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.

Mar 13 '06 #5
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('@')])"
Mar 14 '06 #6
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. :)

Mar 15 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
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
17
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...
5
by: C Gillespie | last post by:
Hi, Does anyone know of any examples on how (& where) to use staticmethods and classmethods? Thanks Colin
16
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)
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
6
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...
3
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
14
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...
3
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()...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.