>>> import __builtin__
>>> class MyClass:
... def __init__(self):
... self.a = 2
... __builtin__.a = self.a
...
>>> myclass = MyClass()
>>> myclass.a = 4
>>> a = 2
>>> print a
2
>>> print myclass.a
4
I would expect a and myclass.a to have the same value. Why don’t they?
PS. Why you would use __builtin__ like this is another question.
Python does not have references. Assigning a value e.g. using
=rebinds the name.If you want such behaviour you need a mutable object, e.g. a list with one element:
However, reconsider if you really want to put stuff like this into
__builtins__.