02 March 2018

python is referential

this is often misunderstood about python, especially if you are accustomed to other languages, such as C/C++.

>>> class thing(object):
...     def __init__(self, some):
...         self.some = some
...     def add_on(self, addition):
...         self.some = self.some + addition
... 
>>> a = thing("hello")
>>> print(a.some)
hello

>>> b = a
>>> print(b)
<__main__.thing object at 0x7f9d267cf7f0>
>>> print(a)
<__main__.thing object at 0x7f9d267cf7f0>
>>> print(b.some)
hello

>>> a.add_on(", world")
>>> print(a.some)
hello, world
>>> print(b.some)
hello, world

>>> print(a)
<__main__.thing object at 0x7f9d267cf7f0>
>>> print(b)
<__main__.thing object at 0x7f9d267cf7f0>