我开始使用更多Python3的打字支持,我希望能够注释作为替代构造函数的int userInput = 0;
int now = time(0);
int later = 0;
int elapsed = 0;
do {
later = time(0);
elapsed = later - now;
cout << "Enter number" <<endl;
cin >> userInput;
switch (userInput)
{
case 1:
elapsed = 0;
break;
}
} while (elapsed < 4 && !_kbhit());
if (elapsed == 4)
{
system("cls");
cout << "Too Slow!! Now You're on the Menu!";
return 0;
}
的返回类型。
以下是一个最小的例子;如果我包含注释失败的话:
staticmethods
def from_other_datastructure(json_data: str) -> MyThing:
NameError: name 'MyThing' is not defined
那么在为类注释定义类之前如何引用类呢?
答案 0 :(得分:2)
发布后我发现了正确答案 - 前向参考可以定义为字符串。
所以正确的答案很简单,并且PyCharm接受了奖励:
@staticmethod
def from_other_datastructure(json_data: str) -> 'MyThing':
return MyThing(
[int(d) for d in json_data.split(',')]
)
https://www.python.org/dev/peps/pep-0484/#forward-references