很多C++程序都有以下语句:
int n;
或
char ch;
但Python如何实现?我们只会这样:
n = 123
或
ch = 'a'
Python提供了这样一种办法,即把:
n = 123
变成
n: int
但是这样必须马上给它赋一个初值(它本身没有初值),例如运行下面的代码:
n: int
print(n)
结果:
Traceback (most recent call last):
File "c:/Users/eh/test_first_value.py", line 10, in <module>
print(i)
NameError: name 'i' is not defined
在诸如VSCode里,预显示变量类型是很方便的: