I defined such simple class as below:
>>> class myclass(object):
name = "google"
And I am creating an instance to call name attribute
>>> a = myclass()
>>> a.name
'google'
and also calling it directly from the class itself.
>>> myclass.name
'google'
So far (as I am having background with C# and Java) I will call myclass.name static invocation and calling static members from instances are fine.
But what I am observing is till I set a.name with another value, changing myclass.name is also affecting a.name. Once I set a.name, changing myclass.name doesn't affect the a.name any more and they begin to have different values.
You can see the whole experiment below:
>>> class myclass(object):
name = "google"
>>> myclass.name
'google'
>>> a = myclass()
>>> a.name
'google'
>>> myclass.name = "yahoo"
>>> a.name
'yahoo'
>>> myclass.name = "hello world"
>>> a.name
'hello world'
>>> myclass.name
'hello world'
>>> myclass.name = "another trick"
>>> myclass.name
'another trick'
>>> a.name
'another trick'
>>> a.name = "I changed the value of a"
>>> a.name
'I changed the value of a'
>>> myclass.name
'another trick'
>>> myclass.name = "changed again"
>>> a.name
'I changed the value of a'
I would like someone to explain the reason behind this behavior.
Thanks.