所以我关注Python's Super Considered Harmful,然后去测试他的例子。
但是,Example 1-3,它应该显示在处理期望不同参数的super
方法时调用__init__
的正确方法时,不能正常工作。
这就是我得到的:
~ $ python example1-3.py
MRO: ['E', 'C', 'A', 'D', 'B', 'object']
E arg= 10
C arg= 10
A
D arg= 10
B
Traceback (most recent call last):
File "Download/example1-3.py", line 27, in <module>
E(arg=10)
File "Download/example1-3.py", line 24, in __init__
super(E, self).__init__(arg, *args, **kwargs)
File "Download/example1-3.py", line 14, in __init__
super(C, self).__init__(arg, *args, **kwargs)
File "Download/example1-3.py", line 4, in __init__
super(A, self).__init__(*args, **kwargs)
File "Download/example1-3.py", line 19, in __init__
super(D, self).__init__(arg, *args, **kwargs)
File "Download/example1-3.py", line 9, in __init__
super(B, self).__init__(*args, **kwargs)
TypeError: object.__init__() takes no parameters
object
本身似乎违反了文档中提到的最佳做法之一,即使用super
的方法必须接受*args
和**kwargs
。
现在,显然Knight先生希望他的例子可以工作,所以在最近的Python版本中这是改变了吗?我检查了2.6和2.7,两者都失败了。
那么处理这个问题的正确方法是什么?
答案 0 :(得分:82)
有时两个类可能有一些共同的参数名称。在这种情况下,您无法弹出**kwargs
之外的键值对或从*args
中删除它们。相反,您可以定义一个与Base
不同的object
类,吸收/忽略参数:
class Base(object):
def __init__(self, *args, **kwargs): pass
class A(Base):
def __init__(self, *args, **kwargs):
print "A"
super(A, self).__init__(*args, **kwargs)
class B(Base):
def __init__(self, *args, **kwargs):
print "B"
super(B, self).__init__(*args, **kwargs)
class C(A):
def __init__(self, arg, *args, **kwargs):
print "C","arg=",arg
super(C, self).__init__(arg, *args, **kwargs)
class D(B):
def __init__(self, arg, *args, **kwargs):
print "D", "arg=",arg
super(D, self).__init__(arg, *args, **kwargs)
class E(C,D):
def __init__(self, arg, *args, **kwargs):
print "E", "arg=",arg
super(E, self).__init__(arg, *args, **kwargs)
print "MRO:", [x.__name__ for x in E.__mro__]
E(10)
产量
MRO: ['E', 'C', 'A', 'D', 'B', 'Base', 'object']
E arg= 10
C arg= 10
A
D arg= 10
B
请注意,要使其正常工作,Base
必须是MRO中的倒数第二个类。
答案 1 :(得分:21)
如果你有很多遗产(这里就是这种情况),我建议你使用**kwargs
传递所有参数,然后在使用它们之后再传递pop
(除非你需要他们在上层阶段。)
class First(object):
def __init__(self, *args, **kwargs):
self.first_arg = kwargs.pop('first_arg')
super(First, self).__init__(*args, **kwargs)
class Second(First):
def __init__(self, *args, **kwargs):
self.second_arg = kwargs.pop('second_arg')
super(Second, self).__init__(*args, **kwargs)
class Third(Second):
def __init__(self, *args, **kwargs):
self.third_arg = kwargs.pop('third_arg')
super(Third, self).__init__(*args, **kwargs)
这是解决这类问题的最简单方法。
third = Third(first_arg=1, second_arg=2, third_arg=3)
答案 2 :(得分:5)
正如Python's super() considered super中所解释的那样,一种方法是让课程吃掉它所需要的论点,然后传递其余的论据。因此,当调用链到达object
时,所有参数都被吃掉了,object.__init__
将被调用而没有参数(正如它所期望的那样)。所以你的代码应该是这样的:
class A(object):
def __init__(self, *args, **kwargs):
print "A"
super(A, self).__init__(*args, **kwargs)
class B(object):
def __init__(self, *args, **kwargs):
print "B"
super(B, self).__init__(*args, **kwargs)
class C(A):
def __init__(self, arg, *args, **kwargs):
print "C","arg=",arg
super(C, self).__init__(*args, **kwargs)
class D(B):
def __init__(self, arg, *args, **kwargs):
print "D", "arg=",arg
super(D, self).__init__(*args, **kwargs)
class E(C,D):
def __init__(self, arg, *args, **kwargs):
print "E", "arg=",arg
super(E, self).__init__(*args, **kwargs)
print "MRO:", [x.__name__ for x in E.__mro__]
E(10, 20, 30)