In your first example, the class mytest
has a tricks
member, shared by all its instances:
class mytest:
name = "test1"
tricks = list()
def __init__(self,name):
...
self.tricks.append(name)
In your second example, however, mytest
instances additionnally have a tricks
member:
class mytest:
name = "test1"
tricks = list()
def __init__(self,name):
...
self.tricks = [name]
...
The self.tricks = [name]
statement gives an attribute named tricks
to self
, that is, the mytest
instance.
The class still has a common tricks
member.
When calling instance.tricks
, Python first looks for a tricks
member in instance.__dict__
. If it does not find any, it looks for a tricks
member in type(instance).__dict__
.
Therefore, instances of your first example have no tricks
attribute, but Python will give you the mytest.tricks
they all share.
On the other hand, instances of your second example do have their own tricks
attribute, and Python will return this one.