I know Python does not work with references, unlike Perl and others, but is it possible to create a simple variable whose values are linked, because they reference the same memory address? I know shallow copies of lists and dictionaries contain values that reference the same address.
>>> foo = 1
>>> bar = foo
>>> bar = 0
>>> foo
1
>>> foo = [1,2]
>>> bar = foo
>>> bar[1] = 0
>>> foo
[0,2]
Cf. in Perl
$ref = 1
$foo = \$ref
$bar = $foo
print $$bar //gives 1
$$foo = 0
print $$bar //gives 0
The reason why is that I am curious to how could it be done/hacked in Python. To give a concrete example is that I would have liked to have given a class two "synonymous" attributes. I suppose that one could pull off a shenanigan like this (untested, sorry if wrong) by masking the second value:
class fake:
def __init__(self,template):
self.ref = template # keep alive
self.__dict___ = template.__dict__
self.__class___ = template.__class__
def __getitem__(self):
return self.ref.__getitem__
def __setitem__(self,value):
self.ref.__setitem__(value)
But that is not quite in the spirit of what I was curious about, but if a hack is the only way, I'll go for that and would love to know the best way.