如何解决在类构造函数中表达兄弟嵌套类的类型提示
我在用 pycharm 编码时使用了 python 的类型提示系统,我相信它使用了 typing
模块,但是我有一个场景,pycharm 给我一个错误,我找不到关于如何的答案使其在线:
from typing import List
class Something:
class A(object):
def __init__(self,d: int) -> None:
self.data = d
class B(object):
def __init__(self,inListStr: List[str],inListA: List[A]): # "A" here is marked as "Unresolved Reference". Something.A does not fix the issue either
self.list_of_str = inListStr
self.list_of_a = inListA
def __init__(self,inB: B): #B here is accepted ok
self.data_b = inB
你知道我如何正确输入 inListA
以成为“列表的 As”类型吗?
解决方法
使用 Something.A
,但将其括在引号中:
...
def __init__(self,inListStr: List[str],inListA: List['Something.A']):
...
Python 解释器无法在代码中的那个点计算 A
或 Something.A
。通过将其设为字符串,类型检查器仍然可以确定类型,同时避免运行时评估。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。