У меня есть некоторые проблемы в решении наследования классов.
Ниже приведен короткий скрипт, который я сделал, чтобы очистить свой разум.
Короче говоря, я ожидал, что смогу получить доступ к каждому методу init классано, похоже, это не так.
Что я могу сделать, чтобы разобраться в этом вопросе?
import sys
class A():
def __init__(self, *args, **kwargs):
s = "I'm the class A __init__ method but it seems I'm calling the _str_ method of C as: {0}".format(self.__str__())
print(s)
print("these are **kwargs k,v ")
for k, v in kwargs.items():
print(k, v)
print("Goodby class A() init;)")
def __str__(self):
return "{0}".format(self.__class__.__name__, )
def callback_a(self):
print("I'm a callback from A but my self.__class__.__name__ appear as {0}?".format(self.__class__.__name__, ))
class B():
def __init__(self, *args, **kwargs):
s = "I'm the class B __init__ method but appear as {0}".format(self.__str__())
print(s)
for k, v in kwargs.items():
print(k, v)
def callback_b(self):
print("I'm a callback from B but my self.__class__.__name__ appear as {0}".format(self.__class__.__name__, ))
def __str__(self):
return "class: %s" % (self.__class__.__name__, )
class C(A, B,):
def __init__(self, *args, **kwargs):
super(C, self).__init__(*args, **kwargs)
def __str__(self):
return "This is the MRO of {0} {1}".format(self.__class__.__name__, [x.__name__ for x in C.__mro__])
def main():
args = []
for i in sys.argv:
args.append(i)
kwargs = {'gallahad': 'the pure', 'robin': 'the brave'}
foo = C(*args, **kwargs)
print(foo)
foo.callback_a()
foo.callback_b()
input('end')
if __name__ == "__main__":
main()
относительно