What is a function parameter =[] for?

Chris Angelico rosuav at gmail.com
Tue Nov 24 11:38:05 EST 2015


On Wed, Nov 25, 2015 at 3:28 AM, Random832 <random832 at fastmail.com> wrote:
> On 2015-11-24, Chris Angelico <rosuav at gmail.com> wrote:
>> On Wed, Nov 25, 2015 at 1:24 AM, Antoon Pardon
>><antoon.pardon at rece.vub.ac.be> wrote:
>>>> Start thinking of it as a constructor call rather than a literal, and
>>>> you'll get past most of the confusion.
>>>
>>> That doesn't change the fact it does look like a literal and not like
>>> a constructor.
>>
>> Neither of them is a literal, even though one of them isn't even
>> constructing a list. Tuples may be constant, but they still don't have
>> a literal form.
>
> How do you define "literal form"? I define it as any syntax that
> can participate in ast.literal_eval (And I consider [...] to be a
> literal form regardless of whether the ... values are literals or
> not). I don't think "Start thinking of it as a constructor call
> rather than a literal" is helpful, since it just hides one's
> confusion about what a literal is.

https://docs.python.org/3/reference/lexical_analysis.html#literals

ast.literal_eval can handle a number of things which are not literals,
as long as they use nothing but literals combined by a restricted set
of operators. For instance, Python has no complex literals, only
imaginary ones - but:

>>> ast.literal_eval("1+2j")
(1+2j)

And Python certainly doesn't have "expression literals", yet:

>>> ast.literal_eval("[1,1+1,3]")
[1, 2, 3]

Its support for list display equally doesn't make that into a literal.

> The Python documentation itself seems to assume that "literal"
> should only be used for things that are a single token, though I
> have no idea where this thinking comes from.

Probably the grammar. In other words, it's part of the language's very
definition.

ChrisA



More information about the Python-list mailing list