[Tutor] "=" invalid syntax ? [Assignment grammar stuff]

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Sun Feb 1 00:19:44 EST 2004

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:


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 "=")+


    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
>>> y1

Hope this helps!

More information about the Tutor mailing list