テストケースは千の言葉に値すると私は信じています:
#!/usr/bin/env python3
def generate_a(key):
class A(object):
def method(self):
return {'key': key,}
return A
BaseForB = generate_a(1337)
class B(BaseForB):
def method(self):
dict = super(BaseForB, self).method()
dict.update({'other_key': 0,})
return dict
EXPECTED = {'other_key': 0, 'key': 1337,}
RESULT = B().method()
if EXPECTED == RESULT:
print("Ok")
else:
print("EXPECTED: ", EXPECTED)
print("RESULT: ", RESULT)
これにより、次のことが発生します。
AttributeError: 'super' object has no attribute 'method'
問題は、どのように実行するかA.method()
ですB.method()
(私がやろうとしたことsuper()
)
編集
より適切なテストケースは次のとおりです。
#!/usr/bin/env python3
def generate_a(key):
class A(object):
def method(self):
return {'key': key,}
return A
class B(object):
def method(self):
return {'key': 'thisiswrong',}
BaseForC = generate_a(1337)
class C(B, BaseForC):
def method(self):
dict = super(C, self).method()
dict.update({'other_key': 0,})
return dict
EXPECTED = {'other_key': 0, 'key': 1337,}
RESULT = C().method()
if EXPECTED == RESULT:
print("Ok")
else:
print("EXPECTED: ", EXPECTED)
print("RESULT: ", RESULT)
問題は、興味のある親クラスをどのように選択するかです。