Can I reference 1 instance of an object by more names ? rephrase
stef
s.mientki at id.umcn.nl
Wed May 23 07:54:14 EDT 2007
Bruno Desthuilliers wrote:
> stef a écrit :
>> 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).
>
> Then you want a property (aka computed attribute).
>
>> 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
>
> You can have something working the same way using a property, but
> that's how far you'll get - if you hoped to be able to automagically
> rebind name2 when rebinding name1, then too bad, because python wont
> let you do so. You have to understand that
> name = obj
> is totally different from
> name.attr = obj
> or
> name[index] = obj
>
> In the first case, this is *really* a binding, and that's one of the
> few things that Python won't let you mess with. In the two last cases,
> it's in fact a method call - as the use of __[get|set]item__ should
> make obvious.
>
> here's an example using a property:
>
> class cpu_ports(object):
> def __init__(self, value=0):
> self._d = value
> @apply
> def value():
> def fset(self, value):
> print 'vv'
> self._d = value
> def fget(self):
> return self._d
> return property(**locals())
>
> 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.value = 25 # assign a value to the instance
> print name1, name2 # both references are OK
>
> name2.value = 26 # assign another value through the other name
> print name1, name2 # both reference are OK
>
> name2.value = name1.value + 13
> print name1, name2 # both reference are OK
>
> And that's about as far as you can go (without rewriting Python I mean).
thanks Bruno for your clear explanation,
I was "afraid" that this would be the only solution,
and now I'm sure,
I can implement it in this way (which is not really a problem).
I just found a Python compiler for PICs (Pyastra),
I still have to study it, but it might be a good idea,
to translate JAL to Python, using the exact syntax of Pyastra.
thanks again,
Stef Mientki
More information about the Python-list
mailing list