I have defined a class Listener
and created a dictionary of Listener
objects. Each listener has an id
to identify them, and a list of
Is this a subtlety of Python I'm not understanding?
It's not subtle, it's quite simple; unlike in other languages which confuse the issue, in Python everything you declare inside the class belongs to the class. This is natural, since classes are objects (like everything else), and thus a perfectly valid place to attach things. Thus, all those methods belong to the class (instead of being somehow magically copied to each instance), and so do the data attributes.
Each listener has an
id
to identify them
Yes, because you attach one to each instance in the __init__
. This has nothing to do with the id
that belongs to the class - except that when you look up id
via an instance, the instance's own id
will be found, hiding the one belonging to the class.
and a list of artists they listen to, artists = []
When you look up artists
via the class, however, the class' artists
will be found, because the instance doesn't have one.
Adding something to the artists list adds it for all instances of the Listener class
No; it's added to the class itself, which is where things are looked for when they aren't found in the instance.
Keep in mind that if you made a direct assignment like self.artists = []
on an instance later, that instance would get its own list hiding the class' list. Other instances would not, because that code didn't get run on the other instances.
You don't want the members declared inside the class, but just set in the __init__
method:
class Listener:
def __init__(self, id):
self.id = id
self.artists = []
def addArtist(self, artist, plays):
print self.id # debugging...
print "pre: ", self.artists
self.artists.append(artist)
print "post: ", self.artists
If you have a class like
class A:
x=5
Then x is a member of the class and not a member of instances of that class. This can be confusing, since python lets you access class members through the instance:
>>> a=A()
>>> print a.x
5
But you can also access it through the class itself:
>>> print A.x
5
It would even appear that this works properly:
>>> a1=A()
>>> a2=A()
>>> a1.x=6
>>> print a1.x
6
>>> print a2.x
5
but what has actually happened is that you've put a new x into the a1 instance, which will be printed instead of the class member, which still has its original value:
>>> print A.x
5
You only start to see a difference when you have something that can be changed, like a list:
class A:
l=[]
>>> a1=A()
>>> print a1.l
[]
>>> a2=A()
>>> print a2.l
[]
>>> a1.l.append(5)
>>> print a1.l
[5]
>>> print a2.l
[5]
>>> print A.l
[5]