[Python-Dev] chained assignment weirdity

Devin Jeanpierre jeanpierreda at gmail.com
Tue Nov 6 09:54:08 CET 2012


On Tue, Nov 6, 2012 at 1:18 AM, Chris Withers <chris at simplistix.co.uk> wrote:
> Hi All,
>
> I bumped into this using Michael Foord's Mock library.
> It feels like a bug to me, but thought I'd ask here before logging one in
> the tracker in case people know that we won't be able to fix it:
>
> On 05/11/2012 13:43, Michael Foord wrote:
>>
>>
>>>>>>> class Foo(object):
>>>>
>>>> ...  def __setattr__(s, k, v):
>>>> ...   print k
>>>> ...
>>>>>>>
>>>>>>> f = Foo()
>>>>>>>
>>>>>>> f.g = f.x = 3
>>>>
>>>> g
>>>> x
>>>

It's terrible and counter-intuitive, but it is documented. See:
http://docs.python.org/2/reference/simple_stmts.html#assignment-statements

Notice that it proclaims that target lists are assigned to from left
to right. That's the behavior you've noticed.

On the other hand, one might easily misread this piece of
documentation, which implies right to left assignment:
http://docs.python.org/2/reference/expressions.html#evaluation-order

But the last note doesn't look normative to me.

(I'm not a dev, just trying to be helpful.)

-- Devin


More information about the Python-Dev mailing list