Python 3 print without parenthesis

前端 未结 8 1743
一整个雨季
一整个雨季 2020-11-30 01:27

The print used to be a statement in Python 2, but now it became a function that requires parenthesis in Python 3.

Is there anyway to suppress these par

8条回答
  •  感动是毒
    2020-11-30 02:00

    Although you need a pair of parentheses to print in Python 3, you no longer need a space after print, because it's a function. So that's only a single extra character.

    If you still find typing a single pair of parentheses to be "unnecessarily time-consuming," you can do p = print and save a few characters that way. Because you can bind new references to functions but not to keywords, you can only do this print shortcut in Python 3.

    Python 2:

    >>> p = print
      File "", line 1
        p = print
                ^
    SyntaxError: invalid syntax
    

    Python 3:

    >>> p = print
    >>> p('hello')
    hello
    

    It'll make your code less readable, but you'll save those few characters every time you print something.

提交回复
热议问题