Can I reference 1 instance of an object by more names ? rephrase

stef s.mientki at id.umcn.nl
Wed May 23 06:55:32 EDT 2007


thanks Guys for your information,

indeed you're all quit right,
but I think I've not correctly described my problem :-(

I need to have 2 (or more) names, that references the same instance of 
an object,
and in assigning a value to the object (or to some property in the object),
I need to do extra activities (like changing some global variables).

Now if I use  a "container type object", without actual using the index 
of the container object,
I get things working OK.
But now I have to use a dummy index, if I use the object in assignments, 
see program below.
Is there another way, without using the dummy index, to achieve the same 
results ?

thanks,
Stef Mientki

<Python>
class cpu_ports(object):
    def __init__(self, value=0):
        self._d = value
    def __setitem__(self, index, value):
      print 'vv'
      self._d = value
    def __getitem__(self, index):
      return self._d
    def __repr__(self):
      return str(self._d)

name1 = cpu_ports()     # create an instance
name2 = name1            # refer with a second name to the same instance
print name1, name2      # test ok

name1[0] = 25               # assign a value to the instance
print name1, name2      # both references are OK

name2[0] = 26               # assign another value through the other name
print name1, name2      # both reference are OK

name2[0] = name1[0] + 13  # use both names at either side of an assignment
print name1, name2            # both references still OK
</Python>



More information about the Python-list mailing list