Python 脚本
'''
a
'''
from __future__ import print_function
效果很好(即什么也不做),但是
'''
a
'''
'''
b
'''
from __future__ import print_function
原因:
File "C:\test.py", line 8
from __future__ import print_function
SyntaxError: from __future__ imports must occur at the beginning of the file
Why?
https://docs.python.org/2/reference/simple_stmts.html#future https://docs.python.org/2/reference/simple_stmts.html#future说:
future 语句必须出现在模块顶部附近。唯一的
可以出现在 future 语句之前的行有:
- 模块文档字符串(如果有),
-
comments,
-
空行, and
- 其他未来的陈述。
第二个示例仅包含注释和前面的空行from __future__ import print_function
,但它不起作用。
我使用Python 2.7。
...这似乎与我给出的第二个例子矛盾。
不,因为这些不是注释,而是字符串。
第一个字符串作为文档字符串从代码中删除,但第二个字符串成为代码中由字符串本身组成的语句。__future__
进口must之前all与代码相关的行,即使是那些没有效果的行。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)