why no ++?

Alex Martelli aleaxit at yahoo.com
Sat Aug 11 18:17:41 EDT 2001


"Stefan Schwarzer" <s.schwarzer at ndh.net> wrote in message
news:3B75B3C0.965E6E2E at ndh.net...
    ...
> > I'm not sure what you mean, since 'lvalue' is not a Python
> > concept, but...:
> >
> >     a=b,c,d=e='wow'
>
> Nice, but seems like "obfuscated Python" to me ;-)
>
> >>> a,b,c,d,e
> ('wow', 'w', 'o', 'w', 'wow')
>
> I haven't yet figured out how it works. Could you please add parantheses
to
> the above expression or clarify otherwise how it works?

I guess I could parenthesize
    a = (b,c,d) = e = 'wow'
but I don't know if that would help.  Neither statement seems
all that obfuscated to me...:-).

The point is that (simple) assignment in Python has the syntax:
    <LHS>{1 or more times} <RHS>
The right-hand side, RHS, is one or more expressions (if more
than one, comma is the separator, and a tuple is implicitly
packed).  Each LHS is:
    <target> =
where target is one or more references (if more than one,
comma is the separator, and a tuple is implicitly unpacked).
(Actually, there are more complicated possibilities, but THOSE
would feel rather obfuscated to me:-).  A 'reference' here may
be a variable, or it may be an indexing, slicing, attribute access.

Back to the example I was giving (to ask for clarification of
the assertion that a Python statement has "just one lvalue"):
we have an assignment with three targets.  Two, a and e,
are just variables.  One, b,c,d [which could be parenthesized
as (b,c,d)] is made up of three references (variables) and
thus implies the unpacking of a sequence of three items.
And sure enough, on the RHS we do have a sequence of
three items (a string of three characters), so each item is
bound to one of the variables, in the same order.


Alex






More information about the Python-list mailing list