Python 3000 idea: reversing the order of chained assignments

Mark T nospam at nospam.com
Thu Mar 22 00:36:10 EDT 2007


"Alex Martelli" <aleax at mac.com> wrote in message 
news:1hvcadb.134pbvp1y39wi3N%aleax at mac.com...
> John Nagle <nagle at animats.com> wrote:
>
>> Marcin Ciura wrote:
>>
>> > Neither would I. I must have expressed myself not clearly enough.
>> > Currently
>> > x = y = z
>> > is roughly equivalent to
>> > x = z
>> > y = z
>> > I propose to change it to
>> > y = z
>> > x = z
>>
>> Actually, it is equivalent to
>>
>>       y = z
>>       x = y
>
> Not really:
>
>>>> class chatty(object):
> ...   def __init__(self): self.__dict__['__hide'] = {}
> ...   def __setattr__(self, name, value):
> ...     print 'sa', name, value
> ...     self.__dict__['__hide'][name] = value
> ...   def __getattr__(self, name):
> ...     print 'ga', name
> ...     return self.__dict__['__hide'].get(name)
> ...
>>>> c = chatty()
>>>> x = c.zop = 23
> sa zop 23
>>>>
>
> As you can see, there is no read-access to c.zop, which plays the role
> of y there.
>
>
> Alex

This is interesting:

>>> class Test(object):
...   def __getattribute__(self,n):
...     print 'reading',n
...     return object.__getattribute__(self,n)
...   def __setattr__(self,n,v):
...     print 'writing',n,v
...     return object.__setattr__(self,n,v)
...
>>> x=Test()
>>> x.a=1; x.b=2; x.c=3
writing a 1
writing b 2
writing c 3
>>> x.a=x.b=x.c
reading c
writing a 3
writing b 3
>>>

I wouldn't have expected "a" to be assigned first in a right-to-left parsing 
order.  The result is the same in any case.

-Mark T. 




More information about the Python-list mailing list