14

Possible Duplicate:
Static class variables in Python
Python OOP and lists

just wondering if I could get some help on this.

I am using python, and have hit an obstacle that I can't seem to figure out with a small program I am working on. Here is my problem (using a very simple and unrelated example): I have a class:

class dog: name = '' friends = [] 

I make a couple objects from it:

fido = dog() rex = dog() 

And here is where I get stuck. I don't know why this is happening and I haven't figured it out. I'm assuming my understanding of something is deficient though, any explanation would be great. So here is my problem, if I append one object to the other (which seems like it should work just fine):

fido.friends.append(rex) 

... things mess up. As you can see here:

>>> fido.friends.append(rex) >>> fido.friends [<__main__.dog instance at 0x0241BAA8>] >>> rex.friends [<__main__.dog instance at 0x0241BAA8>] >>> 

That just deosn't make sense to me. Shouldn't only fido.friends have something in it? Even if I make a new object:

rover = dog() 

It has a dog instance in it, which we can see is our 'rex' object.

>>> rex.name = "rex" >>> fido.friends[0].name 'rex' >>> rex.friends[0].name 'rex' >>> rover.friends[0].name 'rex' >>> 

This just isn't making sense, and I'd love some help. I searched around for awhile trying to find an explanation, but didn't. Sorry if there is a similar question I missed.

0

4 Answers 4

7

If each dog should have his own list of friends, you must use instance attributes:

class Dog(object): family = 'Canidae' # use class attributes for things all instances share def __init__(self, name): """ constructor, called when a new dog is instantiated """ self.name = name self.friends = [] def __repr__(self): return '<Dog %s, friends: %s>' % (self.name, self.friends) fido = Dog('fido') rex = Dog('rex') fido.friends.append(rex) print(fido) # <Dog fido, friends: [<Dog rex, friends: []>]> 

What you used were class attributes (the value is shared among instances). More on this:

Sign up to request clarification or add additional context in comments.

1 Comment

Wow, thanks miku! Greatly appreciate that quick, good answer.
3

Variables declared inside of the class, not attached to an instance, are static variables in python.

1 Comment

"static" isn't a word Python uses to distinguish these concepts.
1

To avoid this, put your variable declaration within a __init__ function, like so:

class Dog: def __init__(self): self.name = '' self.friends = [] 

Comments

1

the proper way to achieve what you want to do is the use of the __init__ method:

>>> class dog: def __init__(self): self.f = [] >>> a = dog() >>> b = dog() >>> a.f.append(b) >>> a.f [<__main__.dog instance at 0x02DA6F08>] >>> c = dog() >>> c.f [] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.