Waffling (was Re: single-line terinary operators considered harmful)

Steven Taschuk staschuk at telusplanet.net
Fri Mar 7 04:04:57 CET 2003


Quoth Stephen Horne:
> On Thu, 6 Mar 2003 13:03:50 -0700, Steven Taschuk
> <staschuk at telusplanet.net> wrote:
> 
> >Certainly Python's guaranteed evaluation and execution order is
> >very comfortable.  There is the occasional oddity:
> >	d = {}
> >	i = 3
> >	i = d[i] = i+1
> >has a surprising (to me) result.  But this isn't good style anyway.
> 
> I wondered what you were talking about - until I noticed that...
  [...]
> I'm not sure I understand it, but I imagine it has to do with '='
> being n-ary (n >= 2) non-associative (rather than binary
> right-associative). Maybe the assigns are done left-to-right, even
> though the rightmost item is the source and must be evaluated first?

Exactly.

Now that I look, I see that the language reference (6.3) has a
nice example of a related gotcha:
	x = [0, 1]
	i = 0
	i, x[i] = 1, 2
	print x
Heh.  All good material for an obfuscated Python contest.

-- 
Steven Taschuk                          staschuk at telusplanet.net
"Its force is immeasurable.  Even Computer cannot determine it."
                            -- _Space: 1999_ episode "Black Sun"





More information about the Python-list mailing list