Python 如何从孙子类调用超级方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31232098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call super method from grandchild class?
提问by SeanLabs
I am working with some code that has 3 levels of class inheritance. From the lowest level derived class, what is the syntax for calling a method 2 levels up the hierarchy, e.g. a super.super call? The "middle" class does not implement the method I need to call.
我正在处理一些具有 3 级类继承的代码。从最低级别的派生类开始,调用方法 2 的语法是什么,例如 super.super 调用?“中间”类没有实现我需要调用的方法。
采纳答案by CrazyCasta
Well, this is one way of doing it:
好吧,这是一种方法:
class Grandparent(object):
def my_method(self):
print "Grandparent"
class Parent(Grandparent):
def my_method(self):
print "Parent"
class Child(Parent):
def my_method(self):
print "Hello Grandparent"
Grandparent.my_method(self)
Maybe not what you want, but it's the best python has unless I'm mistaken. What you're asking sounds anti-pythonic and you'd have to explain why you're doing it for us to give you the happy python way of doing things.
也许不是你想要的,但它是最好的 python,除非我弄错了。您所问的问题听起来很反 Python,您必须解释为什么您要这样做,以便我们为您提供快乐的 Python 做事方式。
Another example, maybe what you want (from your comments):
另一个例子,也许你想要什么(来自你的评论):
class Grandparent(object):
def my_method(self):
print "Grandparent"
class Parent(Grandparent):
def some_other_method(self):
print "Parent"
class Child(Parent):
def my_method(self):
print "Hello Grandparent"
super(Child, self).my_method()
As you can see, Parent
doesn't implement my_method
but Child
can still use super to get at the method that Parent
"sees", i.e. Grandparent
's my_method
.
如您所见,Parent
没有实现my_method
但Child
仍然可以使用 super 来获取Parent
“看到”的方法,即Grandparent
's my_method
。
回答by IamnotBatman
If you want two levels up, why not just do
如果你想要两个级别,为什么不直接做
class GrandParent(object):
def act(self):
print 'grandpa act'
class Parent(GrandParent):
def act(self):
print 'parent act'
class Child(Parent):
def act(self):
super(Child.__bases__[0], self).act()
print 'child act'
instance = Child()
instance.act()
# Prints out
# >>> grandpa act
# >>> child act
You can add something defensive like checking if __bases__
is empty or looping over it if your middle classes have multiple inheritance. Nesting super doesn't work because the type of super isn't the parent type.
你可以添加一些防御性的东西,比如检查是否__bases__
为空或者如果你的中间类有多重继承则循环它。嵌套 super 不起作用,因为 super 的类型不是父类型。
回答by anand tripathi
You can do this by following ways
您可以通过以下方式做到这一点
class Grandparent(object):
def my_method(self):
print "Grandparent"
class Parent(Grandparent):
def my_other_method(self):
print "Parent"
class Child(Parent):
def my_method(self):
print "Inside Child"
super(Child, self).my_method()
In this case Child will call base class my_method but base class my_method is not present there so it will call base class of parent class my_method in this way we can call my_method function of grandparent
在这种情况下,Child 将调用基类 my_method 但基类 my_method 不存在,因此它将以这种方式调用父类 my_method 的基类,我们可以调用祖父母的 my_method 函数
Another Way
其它的办法
class Grandparent(object):
def my_method(self):
print "Grandparent"
class Parent(Grandparent):
def my_other_method(self):
print "Parent"
class Child(Parent):
def my_method(self):
print "Inside Child"
super(Parent, self).my_method()
In this way we are directly calling function base class my_method function of the parent class
这样我们就直接调用了父类的函数基类my_method函数
Another way but not pythonic way
另一种方式但不是pythonic方式
class Grandparent(object):
def my_method(self):
print "Grandparent"
class Parent(Grandparent):
def my_other_method(self):
print "Parent"
class Child(Parent):
def my_method(self):
print "Inside Child"
Grandparent.my_method()
In this way we are directly calling my_method function by specifying the class name.
这样我们就是通过指定类名直接调用my_method函数。
回答by Tomasz
This works for me:
这对我有用:
class Grandparent(object):
def my_method(self):
print "Grandparent"
class Parent(Grandparent):
def my_method(self):
print "Parent"
class Child(Parent):
def my_method(self):
print "Hello Grandparent"
super(Parent, self).my_method()
回答by LucianoDemuru
Made and tested in python 3
在 python 3 中制作和测试
class Vehicle:
# Initializer / Instance Attributes
def __init__(self, name, price):
self.name = name
self.price = price
# instance's methods
def description(self):
print("\nThe car {} has a price of {} eur".format(self.name, self.price))
#Object Vehicle
m3 = Vehicle("BMW M3", 40000)
m3.description()
class Camper(Vehicle):
def __init__(self,nome,prezzo,mq):
super().__init__(nome,prezzo)
self.mq=mq
# instance's methods
def description(self):
super().description()
print("It has a dimension of",format(self.mq)+" mq")
#Camper Object(A camper is also a Vehicle)
marcopolo=Camper("Mercede MarcoPolo",80000,15)
marcopolo.description()
Output:
The car BMW M3 has a price of 40000 eur
The car Mercede MarcoPolo has a price of 80000 eur
It has a dimension of 15 mq
输出:
宝马 M3 的价格为 40000 欧元
奔驰马可波罗的价格为 80000 欧元
它的尺寸为 15 平方米