Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: class attributes and instance attributes

Tags:

python

I'm new to python and learned that class attributes are like static data members in C++. However, I got confused after trying the following code:

>>> class Foo:
...     a=1
... 
>>> f1=Foo();
>>> f2=Foo()
>>> f1.a
1
>>> f1.a=5
>>> f1.a
5
>>> f2.a
1

Shouldn't f2.a also equal 5?

If a is defined as a list instead of an integer, the behavior is expected:

>>> class Foo:
...     a=[]
... 
>>> f1=Foo();
>>> f2=Foo()
>>> f1.a
[]
>>> f1.a.append(5)
>>> f1.a
[5]
>>> f2.a
[5]

I looked at Python: Difference between class and instance attributes, but it doesn't answer my question.

Can anyone explain why the difference? Thanks

like image 472
user11869 Avatar asked Sep 25 '11 20:09

user11869


2 Answers

Python's class attributes and object attributes are stored in separate dictionaries. For the object f1, these can be accessed via, respectively, f1.__class__.__dict__ and f1.__dict__. Executing print f1.__class__ is Foo will output True.

When you reference the attribute of an object, Python first tries to look it up in the object dictionary. If it does not find it there, it checks the class dictionary (and so on up the inheritance heirarchy).

When you assign to f1.a, you are adding an entry to the object dictionary for f1. Subsequent lookups of f1.a will find that entry. Lookups of f2.a will still find the class attribute — the entry in the class attribute dictionary.

You can cause the value of f1.a to revert to 1 by deleting it:

del f1.a

This will remove the entry for a in the object dictionary of f1, and subsequent lookups will continue on to the class dictionary. So, afterwards, print f1.a will output 1.

like image 198
intuited Avatar answered Sep 20 '22 06:09

intuited


You're not doing the same thing in your second example. In you first example, you are assigning f1.a a new value:

f1.a = 5

In your second example, you are simply extending a list:

f1.a.append(5)

This doesn't change what f1.a is pointing to. If you were instead to do this:

f1.a = [5]

You would find that this behaves the same as your first example.

But consider this example:

>>> f1=Foo()
>>> f2=Foo()
>>> Foo.a = 5
>>> f1.a
5
>>> f2.a
5

In this example, we're actually changing the value of the class attribute, and the change is visible in all instances of the class. When you type:

f1.a = 5

You're overriding the class attribute with an instance attribute.

like image 41
larsks Avatar answered Sep 23 '22 06:09

larsks