Jump to content
  • Advertisement
Sign in to follow this  

python, classes, empty lists, weirdness (answered)

This topic is 4731 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Let's say I have the following code:
class Foo:
   bar = []

a = Foo()
b = Foo()
a.bar.append(1)
print a.bar, b.bar
it prints ([1], [1]), and I don't understand why. It seems the two instances of Foo are sharing bar. This doesn't happen if you just use, say, an number instead of a list. [Edited by - wendigo23 on June 8, 2005 3:59:04 PM]

Share this post


Link to post
Share on other sites
Advertisement
That's not how you declare an instance variable in Python. That's actually a class variable (akin to static variables in C++ or Java). But if you say it differs when you use a non-list variable, that makes me think the code you posted isn't actually the code you have problems with.

Share this post


Link to post
Share on other sites
This is the exact code entered into the interpreter. I figured it was akin to a static class member in C++, except that it doesn't hold true for numbers.

Share this post


Link to post
Share on other sites
That's because assigning a new number a.bar = 5 really binds a new reference, creating an object member that shadows the class member (numbers are immutable). Further mention of a.bar refer to the object member, while b.bar still means Foo.bar.
On the other hand modifying the contents of the list doesn't create a new list (lists are mutable): you are still modifying the class member Foo.bar.

Share this post


Link to post
Share on other sites
It's because you are defining bar as a static member, instead of as a class instance variable. In python, you must, even within the class methods themselves, reference the object with the "this," or "self" as we call it in python, pointer. The self reference is not implicit as it is in C++. You have to explicitly place it in both your function declarations, and use it inside class members when accessing the instance's properties. You do not have to use it when you call a class method because python will automagically pass a reference to the object when calling one of it's methods.


class Foo:
# this is the class constructor. As you can see, the "self,"
# or "this" in C++, pointer is EXPLICIT. You must use this
# to refer to the instance of the class.
# Python doesn't place any restrictions on what the "self" parameter is
# called. It just can't be a reserved word.
def __init__(self):
# this is the proper way to create a member called bar
self.bar = []

# because you're not using self, you actually are
# creating a static member called bar and assigning
# to it an emtpy list. Thus your assumption was correct,
# both a and b are using the same instance of bar.
bar = []

a = Foo()
b = Foo()
a.bar.append(1)
print a.bar, b.bar








In the example you provided you didn't have a class constructor and you just created your bar under Foo. Python will let you do some pretty funky things, but the rule is that you need to define all class members in the __init__(self) function.

[Edited by - smr on June 8, 2005 3:50:33 PM]

Share this post


Link to post
Share on other sites
Thanks smr, I already understand instance variables. Fruny has explained the issue I was confused about, thanks. Ratings all around!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!