[Python-ideas] Should a single/double quote followed by a left parenthesis raise SyntaxError?

Ron Adam ron3200 at gmail.com
Wed Jul 15 19:27:10 CEST 2015



On 07/15/2015 11:40 AM, Jacob Niehus wrote:
> I recently forgot the '%' between a format string and its tuple of
> values and got "TypeError: 'str' object is not callable." The error
> makes sense, of course, because function calling has higher precedence
> than anything else in the expression, so while:
>
>>>> '%s' 'abc'
>
> yields '%sabc',
>
>>>> '%s' ('abc')
>
> yields a TypeError.
>
> My question is whether this should be caught as a syntax error instead
> of a runtime error.

No, it's a runtime error.

If it's changed there, it would need to be changed in the following  places 
as well.

 >>> 123 (4)
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable

 >>> [1, 2, 3] (4)
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable

 >>> (1, 2, 3) (4)
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
TypeError: 'tuple' object is not callable


But I can see why you would think syntax error would work.

 >>> s = 'efg'
 >>> 'abc' s
   File "<stdin>", line 1
     'abc' s
           ^
SyntaxError: invalid syntax


Making the other cases match that could break existing working code.  And 
only change one of them introduces an inconsistency.

Cheers,
    Ron



More information about the Python-ideas mailing list