What is a function parameter =[] for?

Antoon Pardon antoon.pardon at rece.vub.ac.be
Tue Nov 24 13:45:11 EST 2015


Op 24-11-15 om 18:53 schreef Ian Kelly:
> 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.

I think limiting literals to lexical tokens is too limited. Sure we
can define them like that in the context of the python grammar, but
I don't see why we should limit ourselves to such a definition outside
that context.

I see nothing wrong with regarding -42 as a literal while according
to the python grammar it isn't.

There is nothing wrong with the notion that a literal can be a
compounded value.

But no matter what you want to call it. The dis module shows that
-42 is treated in exactly the same way as 42, which is treated
exactly the same way as () or as (5, 8, 13) which is treated
differently from [] or [5, 8, 13].

Whether you want to call it literals or something else, the fact
remains that (3, 5, 8) is treated like -42 by the CPython interpreter
and [3, 5, 8] is not.

-- 
Antoon.




More information about the Python-list mailing list