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

Chris Angelico rosuav at gmail.com
Fri Jul 17 03:54:19 CEST 2015


On Fri, Jul 17, 2015 at 11:44 AM, Andrew Barnert via Python-ideas
<python-ideas at python.org> wrote:
> To a normal end-user, the things you syntactically call are basically all values (or, if you prefer, all expressions). Calling a value that's not callable is the same error as adding a thing that's not addable or indexing a thing that's not subscriptable, and that's a TypeError. The existing rules makes sense, and fit in with everything else in Python. For example, we don't consider {1}[1] a syntax error even though it's never valid; how is {1}(1) any different?
>

There are other odd cases in the grammar, too, though. Try explaining this one:
>>> x = 1
>>> x.to_bytes(4,"little")
b'\x01\x00\x00\x00'
>>> 1.to_bytes(4,"little")
  File "<stdin>", line 1
    1.to_bytes(4,"little")
             ^
SyntaxError: invalid syntax
>>> (1).to_bytes(4,"little")
b'\x01\x00\x00\x00'

If the grammar can catch an error, great! If it can't, it'll get dealt
with at run-time. Syntax errors don't have to be reserved for
situations that make it impossible to proceed at all.

ChrisA


More information about the Python-ideas mailing list