Lists are weird when they are instance members

Robin Becker robin at
Sat Jan 3 23:58:01 CET 2004

In article <ffHJb.5475$HL3.4277 at>, python
newbie <mesteve_b at> writes

I think you're not grokking that using

class A:
  mylist = []

makes mylist a class attribute ie all instances share the same version
of mylist. To get an instance version you need to assign self.mylist.
That would normally be done using an __init__method. so

class A:
  def __init__(self):
    self.mylist = []

then each instance sets up its own empty list at creation time.

Hope that helps.
Robin Becker

More information about the Python-list mailing list