3

I saw the following Python documentation which says that "define variables in a Class" will be class variables:

"Programmer's note: Variables defined in the class definition are class variables; they are shared by all instances. "

but as I wrote sample code like this:

class CustomizedMethods(object):
    class_var1 = 'foo'
    class_var2 = 'bar'

cm1 = CustomizedMethods()
cm2 = CustomizedMethods()
print cm1.class_var1, cm1.class_var2 #'foo bar'
print cm2.class_var1, cm2.class_var2 #'foo bar'
cm2.class_var1, cm2.class_var2 = 'bar','for'
print cm1.class_var1, cm1.class_var2 #'foo bar' #here not changed as my expectation
print cm2.class_var1, cm2.class_var2 #'bar foo' #here has changed but they seemed to become instance variables.

I'm confused since what I tried is different from Python's official documentation.

5 Answers 5

4

When you assign an attribute on the instance, it is assigned on the instance, even if it previously existed on the class. At first, class_var1 and class_var2 are indeed class attributes. But when you do cm1.class_var1 = "bar", you are not changing this class attribute. Rather, you are creating a new attribute, also called class_var1, but this one is an instance attribute on the instance cm1.

Here is another example showing the difference, although it still may be a bit tough to grasp:

>>> class A(object):
...     var = []
>>> a = A()
>>> a.var is A.var
True
>>> a.var = []
>>> a.var is A.var
False

At first, a.var is A.var is true (i.e., they are the same object): since a doesn't have it's own attribute called var, trying to access that goes through to the class. After you give a its own instance attribute, it is no longer the same as the one on the class.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! This solved my confusion also. Any idea why it is messed up this way in Python ?
@Gasso: Not sure what you mean. Nothing is "messed up". This is the defined and normal behavior for Python.
I said it is messed up because this is not the behavior that a developer would expect, it is confusing. The variable first behaves like a static class variable then behaves like a class member. There are qualifiers for these, it would've been easier if Python made use of them instead of switching "cm1.class_var1" from static to dynamic
1

You're assigning attributes on the instances, so yes, they become instance variables at that point. Python looks for attributes on whatever object you specify, then if it can't find them there, looks up the inheritance chain (to the class, the class's parents, etc.). So the attribute you assign on the instance "shadows" or "hides" the class's attribute of the same name.

Comments

0

Strings are immutable, so the difference between a class and instance variable isn't as noticable. For immutable variables in a class definition, the main thing to notice is less use of memory (i.e., if you have 1,000 instances of CustomizedMethods, there's still only one instance of the string "foo" stored in memory.)

However, using mutable variables in a class can introduce subtle bugs if you don't know what you're doing.

Consider:

class CustomizedMethods(object):
    class_var = {}

cm1 = CustomizedMethods()
cm2 = CustomizedMethods()

cm1.class_var['test'] = 'foo'
print cm2.class_var
  'foo'

cm2.class_var['test'] = 'bar'
print cm1.class_var
   'bar'

Comments

0

When you reassigned the cm2 variables, you created new instance variables that "hid" the class variables.

>>> CustomizedMethods.class_var1 = 'one'
>>> CustomizedMethods.class_var2 = 'two'
>>> print cm1.class_var1, cm1.class_var2
one two
>>> print cm2.class_var1, cm2.class_var2
bar for

Comments

0

Try to

print cm1.__dict__ 
print cm2.__dict__ 

it will be enlightning...

When you ask cm2 for an attribute it first looks among the attributes of the instance (if one matches the name) and then if there is no matching attribute among the class attributes.

So class_var1 and class_var2 are the names of the class attributes.

Try also the following:

cm2.__class__.class_var1 = "bar_foo" 
print cm1.class_var1 

what do you expect?

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.