A Summary: Expression-Assignments. (Very Long)

Christian Tismer tismer at appliedbiometrics.com
Wed May 12 11:09:38 EDT 1999


Adrian Eyre wrote:
> 
> > Unlike C and Perl, in Python the code 'a=b' is a statement, not an
> > expression. That means it does not return any value, [snip]
> 
> Actually, I find this part of Python a bit inconsistent since:
> 
> a=b=1           is valid while
> a=(b=1) is not...
> 
> To me the fact that 'a=b=1' works implies that the 'b=1' bit is
> actually returning a value. This should perhaps be more consistent
> in Python by either making 'a=b=1' illegal, or making 'a=(b=1)' legal.

This is not true. Not Python is inconsistent, but your
understanding of the semantics of multiple assignments in Python.

The meaning of multiple assignments is:
- Evaluate the right hand side and hold it
- for each left hand side target expression
  - evaluate to assignable object
  - store value

Example how this works 
(not recommended to use this side effect, tho:)

a=b=c[a](5).x=1+2

Evaluation of 1+2, temp=3
Assign temp to a
Assign temp to b
Index c by current value which is 3
Call the resulting function with argument 5
Lookup the result's attribute "x"
Assign temp to attribute "x".

It is very consequent, and has nothing to do with a=(b=1) .

Hint: The bytecode show you exactly this. Try
import dis
def f(): a=b=c[a](5).x=1+2
dis.dis(f)

cheers - chris

-- 
Christian Tismer             :^)   <mailto:tismer at appliedbiometrics.com>
Applied Biometrics GmbH      :     Have a break! Take a ride on Python's
Kaiserin-Augusta-Allee 101   :    *Starship* http://starship.python.net
10553 Berlin                 :     PGP key -> http://wwwkeys.pgp.net
PGP Fingerprint       E182 71C7 1A9D 66E9 9D15  D3CC D4D7 93E2 1FAE F6DF
     we're tired of banana software - shipped green, ripens at home




More information about the Python-list mailing list