"from __future__ imports must occur at the beginning of the file": 什么定义了文件的开头?
"from __future__ imports must occur at the beginning of the file": what defines the beginning of the file?
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
为什么?
https://docs.python.org/2/reference/simple_stmts.html#future 表示:
A future statement must appear near the top of the module. The only
lines that can appear before a future statement are:
- the module docstring (if any),
- comments,
- blank lines, and
- other future statements.
第二个例子在from __future__ import print_function
之前只有注释和空行,但它不起作用。
我用的是Python2.7.
... which seems to be in contradiction with the second example I gave.
不,因为那些不是注释,它们是字符串。
第一个字符串作为文档字符串从代码中删除,但第二个字符串成为代码中由字符串本身组成的语句。 __future__
imports must before all code-relevant lines, even those that have no effect.
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
为什么?
https://docs.python.org/2/reference/simple_stmts.html#future 表示:
A future statement must appear near the top of the module. The only lines that can appear before a future statement are:
- the module docstring (if any),
- comments,
- blank lines, and
- other future statements.
第二个例子在from __future__ import print_function
之前只有注释和空行,但它不起作用。
我用的是Python2.7.
... which seems to be in contradiction with the second example I gave.
不,因为那些不是注释,它们是字符串。
第一个字符串作为文档字符串从代码中删除,但第二个字符串成为代码中由字符串本身组成的语句。 __future__
imports must before all code-relevant lines, even those that have no effect.