4

I have a Python class that has a class attribute set to something other than None. When creating a new instance, the changes made to that attribute perpetuates through all instances.

Here's some code to make sense of this:

 class Foo(object):
   a = []
   b = 2

 foo = Foo()
 foo.a.append('item')
 foo.b = 5

Using foo.a returns ['item'] and foo.b returns 5, as one would expect.

When I create a new instance (we'll call it bar), using bar.a returns ['item'] and bar.b return 5, too! However, when I initially set all the class attributes to None then set them to whatever in __init__, like so:

 class Foo(object):
   a = None
   b = None

   def __init__(self):
     self.a = []
     self.b = 2

Using bar.a returns [] and bar.b returns 2 while foo.a returns ['item'] and foo.b returns 5.

Is this how it's suppose to work? I've apparently never ran into this issue in the 3 years I've programmed Python and would like some clarification. I also can't find it anywhere in the documentation, so giving me a reference would be wonderful if possible. :)

0

4 Answers 4

8

Yes, this is how it is supposed to work.

If a and b belong to the instance of Foo, then the correct way to do this is:

class Foo(object):
   def __init__(self):
     self.a = []
     self.b = 2

The following makes a and b belong to the class itself, so all instances share the same variables:

class Foo(object):
   a = []
   b = 2

When you mix the two methods -- as you did in your second example -- this doesn't add anything useful, and just causes confusion.

One caveat worth mentioning is that when you do the following in your first example:

foo.b = 5

you are not changing Foo.b, you are adding a brand new attribute to foo that "shadows" Foo.b. When you do this, neither bar.b nor Foo.b change. If you subsequently do del foo.b, that'll delete that attribute and foo.b will once again refer to Foo.b.

1
  • Ahh, I see the light! I never realized the difference between a class and instance attribute until now! I always assumed class attributes was defined within the class itself (such as the examples) and an instance would be called with something like foo.c = 2 (an on-the-fly definition, if you will). Makes sense! Thanks! Oct 26, 2011 at 14:21
1

Yes, that's exactly what you should expect. When you define a variable on the class, then those attributes are attached to the class, not the instance. You might not always notice that when you assign to an attribute on an instance, the instance picks up the new value, masking the class attribute. Methods that modify in place, like list.append won't give the instance a new attribute, since they just modify existing object, which happens to be an attribute of the class.

Any time every instance of a class should have its own, unique value for an attribute, you should usually set that in the __init__ method, to be sure that it's different for every instance.

only when a class has an attribute that has a sensible default value, and that value is of an immutable type (that cannot be modified in place), such as int or str, should you set attributes on the class.

1
  • Thanks! Like I said in the answer above, I obviously didn't know the difference between class and instance attributes! I guess this is probably one of the caveats of being self-taught and impatient. ;) Oct 26, 2011 at 14:26
1

Yes, it seems surprising in the begining, but it is the way python works, as said in other answers. Three years of python without being slashed by this? You are lucky, very lucky. If I were you I'd check sensitive past code for little animals...

Be also careful with function calls: def func(list=[]) has a similar behavior, that can be surprising.

And, if possible, plug a pylint parse in your commit hooks. Or at least run pylint sometime on your code, it is very instructive, and would probably have told you about this trick (which is not a trick, in fact, it is very logic and orthogonal, just the python way).

0
0

You're confusing a class attribute with an instance attribute. In your first example, there are only the class attributes a and b, but in the second you also have instance attributes with the same names.

It appears that Python will pass through references to an attribute from an instance to a class if there is not instance attribute (I learned something new today!).

You may find this code instructive:

class Foo:
  a = 1
  b = 2

  def __init__(self):
    self.a=3

print Foo.a   # => 1
print Foo().a # => 3
print Foo.b   # => 2
print Foo().b # => 2
1
  • Thanks! I definitely was confusing the two! Oct 26, 2011 at 14:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.