[Tutor] "=" invalid syntax ? [Assignment grammar stuff] (fwd)

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Mon Feb 2 22:10:27 EST 2004



[Forwarding to Tutor --- slightly busy at the moment.  Sorry!]


---------- Forwarded message ----------
Date: Sun, 1 Feb 2004 12:02:11 -0500
From: orbitz at ezabel.com
To: Danny Yoo <dyoo at hkn.eecs.berkeley.edu>
Subject: Re: [Tutor] "=" invalid syntax ?  [Assignment grammar stuff]

Hey thanks, i just learned it was special cased recently and didn't
really understand it that well. Thanks for putting some work into
explaining it. Do you know, off the top of your head, any other special
cased things like that. Another one someone showed me was:

x, y = [1,2]

Also, in an lvalue, if i do
(x, y) = [1,2]

The () don't make it a tuple in such a situtaion do they?


Thanks


On Sat, 31 Jan 2004 21:19:44 -0800 (PST)
Danny Yoo <dyoo at hkn.eecs.berkeley.edu> wrote:

>
>
> On Sat, 31 Jan 2004 orbitz at ezabel.com wrote:
>
> > I just learned this recently, but if you say = is a statement then
> > you would imagine:
> >
> > a = b = 2 to not work. = is actually special cased for this to work.
> > however
> > a = (b = 2) does not work.  There is some 'magic' some people might
> > say to =, but I think a = b = .. is the only excpetion to the = is a
> > statement operator rule.
>
> Hi orbitz,
>
>
> If people are interested in how it's special cased, the Reference
> Manual has the gory details.  *grin*
>
>
> The grammar definition in:
>
>     http://www.python.org/doc/ref/assignment.html
>
> shows that
>
>     assignment_stmt ::= (target_list "=")+   expression_list
>
>
> That is, an assignment statement is made up of a number of targets,
> followed by the expression that will be assigned to those targets.
> Usually, we define a single target, but nothing stops us from defining
> multiple targets.  If we're looking at somthing like:
>
>     a = b = 2
>
> then we can see that
>
>     a = b =     matches     (target_list "=")+
>
> and
>
>     2           matches     expression_list
>
>
>
> If we look at the grammar, we can also see that assignment allows for
> a limited form of pattern matching --- it's possible to say something
> like:
>
> ###
> >>> p1 = (x1, y1) = (3, 17)
> >>> p1
> (3, 17)
> >>> x1
> 3
> >>> y1
> 17
> ###
>
>
> Hope this helps!
>




More information about the Tutor mailing list