[Python-ideas] In call grammar, replace primary with possible_call? (was Re: ...quote followed by a left parenthesis...?)

Chris Angelico rosuav at gmail.com
Thu Jul 16 13:54:01 CEST 2015

On Thu, Jul 16, 2015 at 9:32 PM, Andrew Barnert via Python-ideas
<python-ideas at python.org> wrote:
> On Jul 15, 2015, at 16:03, Terry Reedy <tjreedy at udel.edu> wrote:
>> So here is the proposal: in the call definition, change primary to
>>  possible_call ::= identifier | parenth_form | yield_atom
>>                    | attributeref | subscription | slicing | call
> It would be fun to explain why [1,2]() is a syntax error but (1,2)() a runtime type error, and likewise for ""() vs ("")()...
> Anyway, I honestly still don't get the motivation here. What makes this kind of type error so special that it should be caught at compile time? We don't expect [1,2]['spam'] to fail to compile; what makes this different? It's a lot simpler to say you can try to call any primary, and you'll get a type error if the primary evaluates to something that's not callable.

It makes good sense to subscript a literal list, though:

month_abbr = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec"][month]

Calling a literal list cannot possibly succeed unless you've hacked
something. The only reason that calling a tuple might survive until
runtime is because it's a bit harder to detect, but Terry's original
post included a footnote about further grammar tweaks that would make
that possible.


More information about the Python-ideas mailing list