About the grammar

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Mon Apr 19 02:52:23 EDT 2010


On Sun, 18 Apr 2010 23:29:44 -0700, franck wrote:

> Dear all,
> 
> I'm wondering why in Python's grammar, keyword arguments are specified
> as:
> 
>     argument: ... | test '=' test


Where are you finding that?


> I would have expected something like
> 
>     argument: ... | NAME '=' test
> 
> Indeed, I cannot imagine a case where the keyword is something else than
> an identifier. Moreover, in the Python language reference (see
> http://docs.python.org/reference/expressions.html#grammar-token-
keyword_item)
> one can read:
> 
>     keyword_item ::= identifier "=" expression
> 
> which is what I was expecting.

This tells you that keyword arguments cannot have keywords that aren't 
identifiers:

>>> sum(1=2)
  File "<stdin>", line 1
SyntaxError: keyword can't be an expression


The only thing I can think of is that you're extrapolating from use cases 
like this:


>>> def f(x):
...     return x
...
>>> x=2
>>> f(x=x)
2

But that's just a special case of this:

>>> y=2
>>> f(x=y)
2


Of course, I could be completely misunderstanding what you mean.




-- 
Steven



More information about the Python-list mailing list