Printing without parentheses varying error message using Python 3

后端 未结 4 1146
被撕碎了的回忆
被撕碎了的回忆 2021-02-05 02:22

When I try to use print without parentheses on a simple name in Python 3.4 I get:

>>> print max
Traceback (most recent call last):
  ...
           


        
4条回答
  •  灰色年华
    2021-02-05 03:08

    in additions to those excellent answers, without even looking at the source code, we could have guessed that the print special error message was a kludge:

    so:

    print dfjdkf
               ^
    SyntaxError: Missing parentheses in call to 'print'
    

    but:

    >>> a = print
    >>> a dsds
    Traceback (most recent call last):
      File "", line 1
        a dsds
             ^
    SyntaxError: invalid syntax
    

    even if a == print but at that stage, it isn't evaluated yet, so you get the generic invalid syntax message instead of the hacked print syntax message, which proves that there's a kludge when the first token is print.

    another proof if needed:

    >>> print = None
    >>> print a
    Traceback (most recent call last):
      File "C:\Python34\lib\code.py", line 63, in runsource
        print a
              ^
    SyntaxError: Missing parentheses in call to 'print'
    

    in that case print == None, but the specific message still appears.

提交回复
热议问题