I read somewhere that "if python can't find instance variable it will try to return value of the class variable having the same name"
eg
class Sample:
pi = 10
Now
x1 = Sample()
x2 = Sample()
x1.pi # returns 10
x2.pi # returns 10
x1.pi = 20 # change the value of class variable
x1.pi # return 20 (OK)
x2.pi # still returns 10 :(
Sample.pi # returns 10 :(
What is happening??
As soon as you assign to a name on an instance, it gains an instance attribute that shadows the class attribute.
The only way you can assign to the class attribute is to assign to an attribute of the class, not an attribute of the instance, e.g. if you have an instance, you need to do:
x1.__class__.pi = 20
# If you're on Py3, or on Py2 and x1 is an instance of a new-style class,
# using type(x1) is slightly "nicer" than manually accessing dunder special
# variables, but unfortunately, it doesn't work on old-style class instances
# For new-style class instances though, the following is equivalent:
type(x1).pi = 20
if you want all instances of the same type as x1
to show the change. This gets the class itself from __class__
(or via type
function), then assigns to it.
If you accidentally created an instance attribute and want to expose the class attribute again, you can do:
del x1.pi
which will succeed if an instance attribute named pi
exists, and raise AttributeError
if it does not (it will not delete the class attribute if it exists, you'd need to do del x1.__class__.pi
/del type(x1).pi
to do that).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With