Use of super() with not immediate parent
Is this a legal use of super()?
class A(object):
def method(self, arg):
pass
class B(A):
def method(self, arg):
super(B,self).method(arg)
class C(B):
def method(self, arg):
super(B,self).method(开发者_StackOverflow中文版arg)
Thank you.
It will work, but it will probably confuse anyone trying to read your code (including you, unless you remember it specifically). Don't forget that if you want to call a method from a particular parent class, you can just do:
A.method(self, arg)
Well, "legal" is a questionable term here. The code will end up calling A.method
, since the type given to super
is excluded from the search. I would consider this usage of super
flaky to say the least, since it will skip a member of the inheritance hierarchy (seemingly haphhazardly), which is inconsistent with what I would expect as a developer. Since users of super
are already encouraged to maintain consistency, I'd recommend against this practice.
精彩评论