Curious assignment behaviour

Chris Gonnerman chris.gonnerman at newcenturycomputers.net
Mon Oct 8 08:22:18 EDT 2001


----- Original Message -----
From: "Dale Strickland-Clark" <dale at riverhall.NOSPAMco.uk>


> I observe that the folowing works as some might expect:
>
> x = y = 5
>
> Assigns 5 to both x and y.
>
> However the expression:
>
> y = 5
>
> does not yeild 5
>
>  >>> print y = 5
>  Traceback (  File "<interactive input>", line 1
>      print y = 5
>             ^
>  SyntaxError: invalid syntax
>  >>>
>
> So this is obviously a gludge in assignment.
>
> I'd be much happier if assignment yeilds the value assigned, as it
> does in C (I believe).  It could then be used universaly.

Assignment isn't an expression, it's a statement.  What you are requesting
is asked for all the time; Guido decided against it a long time ago because
constructions like:

    if c = 1:
        ...

are almost certainly not what the programmer had in mind.  C does the
wrong thing; Python throws an error so the programmer can see what he or
she did wrong immediately.

> Also, where is this curious behaviour documented? 6.3 of the Language
> Ref makes no reference to it. (Go on. Prove me wrong!)

The simple fact that it's in section 6 (Statements) instead of 5
(Expressions)
gives the answer.

> Thanks.
> --
> Dale Strickland-Clark
> Riverhall Systems Ltd
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>





More information about the Python-list mailing list