[英]Python 3.6 variable annotations and numeric literals
In the documentation on Python in the section "What's new in Python 3.6" among other things there are presented variable annotations and using underscores in numeric literals. 在Python文档中的“ Python 3.6的新增功能”部分中,除其他外,还提供了变量注释和在数字文字中使用下划线。
However I tried shown examples and not all of them were passed. 但是,我尝试了显示的示例,但并非所有示例都通过了。
Are these examples incomplete and do they require some additional code that is assumed under the hood? 这些示例是否不完整,是否需要幕后假设一些其他代码?
For example this statement 例如此语句
primes: List[int] = []
issues 问题
NameError: name 'List' is not defined
This statement 这个说法
print( 1_000_000_000_000_000 )
is also considered as wrong. 也被认为是错误的。
The first case works if you first import List
from typing
. 如果您首先通过
typing
导入List
,则第一种情况有效。 Most types used with type-hints aren't built-in, they need to be imported first. 大多数与类型提示一起使用的类型不是内置的,它们需要首先导入。
The second case also works if you are running under 3.6
. 如果在
3.6
下运行,第二种情况也适用。 On my machine it correctly prints: 在我的机器上,它可以正确打印:
Python 3.6.2 | packaged by conda-forge | (default, Jul 23 2017, 22:59:30)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-15)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print( 1_000_000_000_000_000 )
1000000000000000
If the error message you receive is: SyntaxError: invalid syntax
you're on 3.5
or less. 如果您收到的错误消息是:
SyntaxError: invalid syntax
,等于或小于3.5
。 If it's SyntaxError: invalid token
you're not using the underscores correctly. 如果是
SyntaxError: invalid token
您未正确使用下划线。 I'm guessing you're receiving the first. 我猜您正在收到第一个。
So, you might want to double check you're running with 3.6
( python -V
). 因此,您可能需要仔细检查您是否正在使用
3.6
( python -V
)运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.