What is a function parameter =[] for?

Ian Kelly ian.g.kelly at gmail.com
Tue Nov 24 12:53:44 EST 2015


On Tue, Nov 24, 2015 at 10:32 AM, Antoon Pardon
<antoon.pardon at rece.vub.ac.be> wrote:
> Op 24-11-15 om 17:56 schreef Ian Kelly:
>
>>
>>> So on what grounds would you argue that () is not a literal.
>>
>> This enumerates exactly what literals are in Python:
>>
>> https://docs.python.org/3/reference/lexical_analysis.html#literals
>>
>> I think it's a rather pedantic point, though. How are nuances of the
>> grammar at all related to user expectations?
>>
>
> I think that enumaration is too limited. The section starts with:
>
>    Literals are notations for constant values of some built-in types.
>
> () satisfies that definition, which is confirmed by the byte code
> produced for it.

Literals are a type of lexical token. All of the literals shown in
that section are, indeed, tokens. Now I would point you to the grammar
specification:

https://docs.python.org/3/reference/grammar.html

And specifically the "atom" rule, which defines both list displays and
list comprehensions (as well as literals) as being atoms.
Specifically, it parses () as the token '(', followed by an optional
yield_expr or testlist_comp, followed by the token ')'. In no way is
that a single token, nor therefore a literal.



More information about the Python-list mailing list