BaseB__init__
関数では、親の作成者ではなく、子の作成者を呼び出しています。親の作者に電話をかけたい場合は、次のBaseB.authors
代わりに使用しself.authors
ます。
class BaseA(object):
authors = ['a', 'b']
author_list = authors
class BaseB(object):
authors = ['a', 'b']
def __init__(self, *arg, **kwargs):
self.author_list = BaseB.authors
class ChildA(BaseA):
authors = ['c', 'd']
class ChildB(BaseB):
authors = ['c', 'd']
child_a = ChildA()
child_b = ChildB()
print(child_a.author_list)
print(child_b.author_list)
>>> ['a', 'b']
>>> ['a', 'b']
ParentBとChildBの作成者を連結する場合は、との両方を使用しBaseB.authors
ますself.authors
。
class BaseA(object):
authors = ['a', 'b']
author_list = authors
class BaseB(object):
authors = ['a', 'b']
def __init__(self, *arg, **kwargs):
self.author_list = BaseB.authors + self.authors
class ChildA(BaseA):
authors = ['c', 'd']
class ChildB(BaseB):
authors = ['c', 'd']
child_a = ChildA()
child_b = ChildB()
print(child_a.author_list)
print(child_b.author_list)
>>> ['a', 'b']
>>> ['a', 'b', 'c', 'd']
オーバーライド
class BaseA(object):
authors = ['a', 'b']
author_list = authors
class BaseB(object):
authors = ['a', 'b']
def __init__(self, *arg, **kwargs):
self.author_list = self.authors
class ChildA(BaseA):
authors = ['c', 'd']
def __init__(self, *arg, **kwargs):
self.author_list = self.authors
class ChildB(BaseB):
authors = ['c', 'd']
child_a = ChildA()
child_b = ChildB()
print(child_a.author_list)
print(child_b.author_list)
>>> ['c', 'd']
>>> ['c', 'd']