【发布时间】:2017-03-15 05:48:46
【问题描述】:
我开始使用更多 Python3 的类型支持,我希望能够注释 staticmethods 作为替代构造函数的返回类型。
下面是一个最小的例子;如果我包含它失败的注释:
def from_other_datastructure(json_data: str) -> MyThing:
NameError: name 'MyThing' is not defined
import typing
class MyThing:
def __init__(self, items: typing.List[int]):
self.items = items
@staticmethod
def from_other_datastructure(json_data: str):
return MyThing(
[int(d) for d in json_data.split(',')]
)
if __name__ == '__main__':
s1 = MyThing([1, 2, 3])
s2 = MyThing.from_other_datastructure("2,3,4")
那么在为类型注解定义之前如何引用一个类呢?
【问题讨论】:
标签: python-3.x