简体   繁体   English

Python 3.6变量注释和数字文字

[英]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.6python -V )运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Python 3.6变量注释提供向后兼容性 - Providing Backwards Compatability with Python 3.6 Variable Annotations Python 3.6 中带元组的格式化字符串文字 - Formatted string literals in Python 3.6 with tuples Python 3.6中的格式化字符串文字是什么? - What are formatted string literals in Python 3.6? Python 3.6 - 如何在 A.Write 语句中格式化变量和文字? - Python 3.6 - How Do I Format Variables and Literals In A .Write Statement? Python 3.6正则表达式产生意外结果(尽管使用字符串文字) - Python 3.6 Regex Producing Unexpected Results (despite using string literals) “void”函数中的NoReturn与None - 在Python 3.6中键入注释 - NoReturn vs. None in “void” functions - type annotations in Python 3.6 为什么 __future__.annotations (PEP 563) 没有向后移植到 python3.6? - Why is __future__.annotations (PEP 563) not backported to python3.6? 如何避免以“0”开头的python数字文字被视为八进制? - how to avoid python numeric literals beginning with “0” being treated as octal? 如何检查“数据框”列中是否包含数值? Python 3.6 - How to check Data Frame columns contains numeric values or not? Python 3.6 如何获取 Python 变量注释? - How to get Python variable annotations?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM