是否可以在Python中声明typew

时间:2019-03-14 14:21:45

标签: python python-3.x

最近,在阅读python 3.7.3文档时,我遇到了我从未见过的这样的声明

name: str
unit_price: float
quantity_on_hand: int = 0

我打开了IDLE(3.6.4)并尝试了此操作。结果是它显然根本不重要,即。未初始化的变量没有类型,即使在“指定”类型之后也可以更改(请参见下面的代码)。那么,这样的声明有什么意义呢?他们有什么改变吗?

>>> value: int
>>> type(value)
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    type(value)
NameError: name 'value' is not defined
>>> value: int = 0
>>> type(value)
<class 'int'>
>>> value = "string"
>>> type(value)
<class 'str'>

0 个答案:

没有答案