[英]Expressing type hint of sibling nested class in class constructor
我在使用 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
。 通過將其設為字符串,類型檢查器仍然可以在避免運行時評估的同時找出類型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.