正在定义的类型对象的类型提示
Type Hinting for objects of type that's being defined
我收到错误:
NameError: name 'OrgUnit' is not defined
class OrgUnit(object):
def __init__(self,
an_org_name: str,
its_parent_org_unit: OrgUnit= None
):
self.org_unit_name = an_org_name
self.parent_org_unit = its_parent_org_unit
def __str__(self):
if self.parent_org_unit:
parent_org_unit_name = self.parent_org_unit.__str__()
return parent_org_unit_name + "->" + self.org_unit_name
else:
return self.org_unit_name
if __name__ == '__main__':
ibm_worldwide = OrgUnit("IBM_Worldwide")
ibm_usa = OrgUnit("IBM_USA", ibm_worldwide)
ibm_asia = OrgUnit("IBM_Asia", ibm_worldwide)
ibm_china = OrgUnit("IBM_China", ibm_asia)
print(ibm_worldwide)
print(ibm_usa)
print(ibm_asia)
print(ibm_china)
我确信这是一个已知的范例,因为它似乎是一个非常常见的分层 class 使用问题(自引用 class)。我知道我可以将 its_parent_org_unit
的类型更改为 object
并且它有效,但这似乎是错误的做法,主要是因为它破坏了我在调用中检查类型的能力。 its_parent_org_unit
更改为类型 object
我得到正确的结果:
IBM_Worldwide
IBM_Worldwide->IBM_USA
IBM_Worldwide->IBM_Asia
IBM_Worldwide->IBM_Asia->IBM_China
我乐于接受想法和建议。做这种事最 "pythonic" 的方法是什么?
PS:这种 "self referencing class" paradigm/problem 的名称是什么,我可以用它来查找其他建议?
你的问题是你想使用类型提示,但你希望这个 class 本身能够接受它自己类型的参数。
类型提示 PEP (0484) 说明您可以使用 string version of the type's name as a forward reference。那里的例子是一个 Tree
数据结构,听起来与这个 OrgUnit
非常相似。
例如,这个有效:
class OrgUnit(object):
def __init__(self,
an_org_name: str,
its_parent_org_unit: 'OrgUnit' = None
):
在 Python 3.7 中,您将能够使用 from __future__ import annotations
激活 postponed evaluation of annotations。这将自动将注释存储为字符串而不是对其求值,因此您可以
from __future__ import annotations
class OrgUnit(object):
def __init__(self,
an_org_name: str,
its_parent_org_unit: OrgUnit= None
):
...
这计划成为 Python 4.0 中的默认设置。
我收到错误:
NameError: name 'OrgUnit' is not defined
class OrgUnit(object):
def __init__(self,
an_org_name: str,
its_parent_org_unit: OrgUnit= None
):
self.org_unit_name = an_org_name
self.parent_org_unit = its_parent_org_unit
def __str__(self):
if self.parent_org_unit:
parent_org_unit_name = self.parent_org_unit.__str__()
return parent_org_unit_name + "->" + self.org_unit_name
else:
return self.org_unit_name
if __name__ == '__main__':
ibm_worldwide = OrgUnit("IBM_Worldwide")
ibm_usa = OrgUnit("IBM_USA", ibm_worldwide)
ibm_asia = OrgUnit("IBM_Asia", ibm_worldwide)
ibm_china = OrgUnit("IBM_China", ibm_asia)
print(ibm_worldwide)
print(ibm_usa)
print(ibm_asia)
print(ibm_china)
我确信这是一个已知的范例,因为它似乎是一个非常常见的分层 class 使用问题(自引用 class)。我知道我可以将 its_parent_org_unit
的类型更改为 object
并且它有效,但这似乎是错误的做法,主要是因为它破坏了我在调用中检查类型的能力。 its_parent_org_unit
更改为类型 object
我得到正确的结果:
IBM_Worldwide
IBM_Worldwide->IBM_USA
IBM_Worldwide->IBM_Asia
IBM_Worldwide->IBM_Asia->IBM_China
我乐于接受想法和建议。做这种事最 "pythonic" 的方法是什么?
PS:这种 "self referencing class" paradigm/problem 的名称是什么,我可以用它来查找其他建议?
你的问题是你想使用类型提示,但你希望这个 class 本身能够接受它自己类型的参数。
类型提示 PEP (0484) 说明您可以使用 string version of the type's name as a forward reference。那里的例子是一个 Tree
数据结构,听起来与这个 OrgUnit
非常相似。
例如,这个有效:
class OrgUnit(object):
def __init__(self,
an_org_name: str,
its_parent_org_unit: 'OrgUnit' = None
):
在 Python 3.7 中,您将能够使用 from __future__ import annotations
激活 postponed evaluation of annotations。这将自动将注释存储为字符串而不是对其求值,因此您可以
from __future__ import annotations
class OrgUnit(object):
def __init__(self,
an_org_name: str,
its_parent_org_unit: OrgUnit= None
):
...
这计划成为 Python 4.0 中的默认设置。