Python 3000 idea: reversing the order of chained assignments

Steve Holden steve at holdenweb.com
Thu Mar 22 04:48:27 EDT 2007


Mark T wrote:
> "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.
> 
It would be nice if your confusion could have been avoided by reading 
the reference manual, but unfortunately the current syntax definition 
doesn't seem to even acknowledge the possibility of multiple assignments 
such as the one under discussion!

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Skype: holdenweb     http://del.icio.us/steve.holden
Recent Ramblings       http://holdenweb.blogspot.com




More information about the Python-list mailing list