0

I am trying to establish the difference between these two methods of creating variables in a class and which method is correct?

class example():
    var = 12

class example2():
    def __init__(self):
        self.var = 34

ob1 = example()
ob2 = example2()

print (ob1.var)
print (ob2.var)

This returns the contents of the variables as expected:

12
34
>>> 

I have searched stackoverflow to see if there is anything on this and couldn't find anything that compared the two, I find its always easier to search when you know the answer.

Thank you

CodeCupboard
  • 1,507
  • 3
  • 17
  • 26
  • 1
    The first is a class attribute which is shared between all instances of the class. The second is an instance attribute and can differ between instances. – Klaus D. Mar 21 '19 at 10:21

2 Answers2

4

The first is a class variable, and the second is an instance variable. You can access a class variable without creating an instance. You can't access an instance variable without creating an instance. This is obvious, because the constructor __init()__ has to be called for class example2 to have var.

>>> example.var
12
>>> example2.var
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: type object 'example2' has no attribute 'var'

Class variables are shared across instances of the same class. This becomes more obvious if you consider the following class:

>>> class dummy():
...   dummy_list = []
...   def __init__(self):
...     self.dummy_list2 = []
...
>>> a = dummy()
>>> b = dummy()
>>> a.dummy_list.append(1)
>>> a.dummy_list2.append(2)
>>> b.dummy_list.append(3)
>>> b.dummy_list2.append(4)
>>> a.dummy_list
[1, 3]
>>> a.dummy_list2
[2]
>>> b.dummy_list
[1, 3]
>>> b.dummy_list2
[4]
>>> dummy.dummy_list
[1, 3]

You can see dummy_list is shared between the two instances, but each instance has their own dummy_list2.

Ignatius
  • 2,745
  • 2
  • 20
  • 32
  • By the way I just came across [this](https://stackoverflow.com/questions/8959097/what-is-the-difference-between-class-and-instance-variables) very good and extensive answer. You should check it out. – Ignatius Mar 21 '19 at 10:28
  • Thank you that makes a lot of sense – CodeCupboard Mar 21 '19 at 10:37
2

The difference is that the var in example is a class variable and example2 is an instance variable. So for example this is valid:

# Change the class value
example.var = 100
#change an instance value
instance = example()
instance.var = 101
# But they will be different
print(instance.var == example.var) # prints False
# but at the beginning they are the same
print(example().var == example.var) # prints True

This is not valid

# this will throw an error
example2.var = 100 

This is valid

instance2 = example2()
instance2.var = 101

For code clarity I'd stay away from the class variable (example) and initialize your instance variables in init unless you have a really good reason.

I really hope I'm not answering someones homework.

fsaint
  • 8,759
  • 3
  • 36
  • 48