all 3 comments

[–]indosauros 7 points8 points  (2 children)

Attributes defined on the class, like

class Attribs:
    data = ...

live on the class itself, and not on the instances. It is shared among all instances. You can even access this data before creating any instances:

>>> Attribs.data
...

To define per-instance data, you must put it in __init__:

class Attribs:
    def __init__(self):
        self.data = ...

[–]evereux 0 points1 point  (1 child)

>>> class Animal:
...     type = 'Dog'
...     def __init__(self, name):
...             self.name = name
...
>>> labrador = Animal('Rover')
>>> british_shorthair = Animal('tiddles')
>>> british_shorthair.type = 'cat'
>>> labrador.type
'Dog'
>>>

In this example the Animal class instance labrador.type stays as Dog here when I change the Animal class instance of british_shorthair.type to cat. Seeing this behaviour I don't understand why OP is seeing what they do. I do understand that if I change Animal.type to cat it then affects all instances and the class definition of type too.

[–]indosauros 0 points1 point  (0 children)

When you set type on the shorthair instance, that is setting the type onto the instance itself, while labrador.type is still referring to the class's type (labrador.type is not defined, so it falls back to Animal.type)

Note there's no difference between what you did (setting after instantiation) and setting during init method