我收到错误:
NameError: name 'OrgUnit' is not defined
Run Code Online (Sandbox Code Playgroud)
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)
Run Code Online (Sandbox Code Playgroud)
我确信这是一个已知的范例,因为它似乎是一个非常常见的分层类使用问题(自引用类).我知道我可以改变的类型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
Run Code Online (Sandbox Code Playgroud)
我对这些想法和建议持开放态度.做这种事情最"pythonic"的方法是什么?
PS:这种"自引用类"范例/问题的名称是什么,我可以用来查找其他建议?