在Python中使用super()关键字的单继承的一个基本示例是什么? [英] What is a basic example of single inheritance using the super() keyword in Python?
本文介绍了在Python中使用super()关键字的单继承的一个基本示例是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我设置了以下类:
class Foo:
def __init__(self, frob, frotz):
self.frobnicate = frob
self.frotz = frotz
class Bar:
def __init__(self, frob, frizzle):
self.frobnicate = frob
self.frotz = 34
self.frazzle = frizzle
推荐答案
在Python中,我可以使用super() = 3.0,像这样:
In Python >=3.0, like this:
class Foo():
def __init__(self, frob, frotz)
self.frobnicate = frob
self.frotz = frotz
class Bar(Foo):
def __init__(self, frob, frizzle)
super().__init__(frob, 34)
self.frazzle = frizzle
a href =http://docs.python.org/3.1/library/functions.html#super =nofollow noreferrer> http://docs.python.org/3.1/library/functions.html#super
Read more here: http://docs.python.org/3.1/library/functions.html#super
编辑:正如在另一个答案中所说,有时只是使用 Foo .__ init __(self,frob,34)
可以是更好的解决方案。 (例如,当使用某些形式的多重继承时)。
As said in another answer, sometimes just using Foo.__init__(self, frob, 34)
can be the better solution. (For instance, when working with certain forms of multiple inheritance.)
这篇关于在Python中使用super()关键字的单继承的一个基本示例是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文