【问题标题】:why do we invoke print after importing print_function (in Python 2.6)为什么我们在导入 print_function 后调用 print(在 Python 2.6 中)
【发布时间】:2011-06-01 10:35:08
【问题描述】:

为了获得 3.0 的打印功能,我们在 Python 2.6 中执行以下操作:

from __future__ import print_function

但是要使用我们调用 print() 而不是 print_function() 的函数。这只是不一致还是有充分的理由?

为什么不如下:

from __future__ import print

【问题讨论】:

标签: python import


【解决方案1】:

在 Python 3 中,关键字 print 已从调用语句更改为调用函数。

所以你现在需要说print(value),而不是说print value,否则你会得到SyntaxError

通过执行import,此更改也将在 Python 2 中生效,因此您可以使用与 Python 3 相同的语法编写程序(至少就print 而言)。

【讨论】:

  • 那么为什么不从 future 导入打印呢?
  • @broiyan:因为这会令人困惑,因为看起来您导入了您没有导入的名称“print”。 __future__ 导入是改变 Python 语法行为方式的魔法。
  • ...另一个问题,您可以一直阅读到最后一个答案以获得简单的解释; 让 python2 与 python3 打印语法一起工作.
【解决方案2】:

原因是,当您从 __future__ 导入时,您实际上只是设置了一个标志,告诉解释器的行为与平时有所不同——在 print_function 的情况下,print() 函数被创建可以代替声明。 __future__ 模块因此是“特殊的”或“神奇的”——它不像通常的模块那样工作。

【讨论】:

  • 磁盘上有一个名为 future 的模块,其中有一个名为 print_function 的东西,但真正的魔法正在其他地方发生。
  • 总结:有这么一个模块,但是你不需要关心它,因为魔术在别处完成。 :)
  • 你能取消该标志设置一次吗?
【解决方案3】:

print_function 是一个FeatureName,不要与print 内置函数本身混淆。 这是一项未来可用的功能,因此您可以使用它可以提供的内置功能。

其他功能包括:

all_feature_names = [
    "nested_scopes",
    "generators",
    "division",
    "absolute_import",
    "with_statement",
    "print_function",
    "unicode_literals",
]

当您将代码迁移到下一个更高版本时,您的程序将保持不变,例如使用更新后的功能而不是 __future__ 版本。另外,如果是函数名或关键字本身,可能会导致解析器混淆。

【讨论】:

    【解决方案4】:

    简单。 print 是 Python 2 中的关键字。

    这样的陈述

    from somewhere import print
    

    将是 Python 2 中的自动 SyntaxError。

    允许(在语法中硬编码)

    from __future__ import print
    

    被认为不值得努力。

    【讨论】:

      【解决方案5】:

      小例子

      >>> print     # Statement.
      
      >>> from __future__ import print_function
      >>> print     # Function object.
      <built-in function print>
      >>> print()   # Function call.
      
      >>>
      

      正如What is __future__ in Python used for and how/when to use it, and how it works from __future__ 中提到的,是改变 Python 解析代码方式的神奇语句。

      from __future__ import print_function 尤其是将 print 从语句更改为内置函数,如上面的交互式 shell 所示。

      为什么 print(1) 在 Python 2 中没有 from __future__ import print_function 也能工作

      因为:

      print(1)
      

      被解析为:

      print (1)
      ^^^^^ ^^^
      1     2
      
      1. print声明
      2. 论据

      代替:

      print( 1 )
      ^^^^^^ ^ ^
      1      2 1
      
      1. print()函数
      2. 论据

      还有:

      assert 1 == (1)
      

      如提及:Python tuple trailing comma syntax rule

      【讨论】:

        【解决方案6】:

        为了完整起见,目前available 的所有功能是:

        +------------------+-------------+--------------+----------------------------------------------------+
        |     feature      | optional in | mandatory in |                       effect                       |
        +------------------+-------------+--------------+----------------------------------------------------+
        | nested_scopes    | 2.1.0b1     |          2.2 | PEP 227: Statically Nested Scopes                  |
        | generators       | 2.2.0a1     |          2.3 | PEP 255: Simple Generators                         |
        | division         | 2.2.0a2     |          3.0 | PEP 238: Changing the Division Operator            |
        | absolute_import  | 2.5.0a1     |          3.0 | PEP 328: Imports: Multi-Line and Absolute/Relative |
        | with_statement   | 2.5.0a1     |          2.6 | PEP 343: The “with” Statement                      |
        | print_function   | 2.6.0a2     |          3.0 | PEP 3105: Make print a function                    |
        | unicode_literals | 2.6.0a2     |          3.0 | PEP 3112: Bytes literals in Python 3000            |
        | generator_stop   | 3.5.0b1     |          3.7 | PEP 479: StopIteration handling inside generators  |
        | annotations      | 3.7.0b1     |          4.0 | PEP 563: Postponed evaluation of annotations       |
        +------------------+-------------+--------------+----------------------------------------------------+
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-04-10
          • 2011-11-09
          • 2011-01-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-11-02
          相关资源
          最近更新 更多