クラスの親と、次のニアコードのような 2 つのサブクラス child1(parent) と child2(parent) があります。(親クラスが何かを行っていることをより適切に示すように編集されています)
class parent(object):
name = None
def __init__(self,e):
# process the common attributes
name = e.attrib['name']
def __new__(cls,e):
if e.attrib['type'] == 'c1':
return child1(e)
elif e.attrib['type'] == 'c2':
return child2(e)
else:
raise
class child1(parent):
extra1 = None
def __init__(self,e):
super(e)
# set attributes from e that are specific to type c1
class child2(parent):
extra2 = None
def __init__(self,e):
super(e)
# set attributes from e that are specific to type c2
目標は、パラメーターの値に基づいて「正しい」クラスを取得できるようにすることです。だから私が言うことができれば、の値が何であるかに応じて、またはどちらかobj = parent(element)
になります。obj
child1
child2
element.attrib['type']