python2.x 3.x的super关键字使用示例

来源:互联网 发布:php分割字符串 编辑:程序博客网 时间:2024/05/22 04:54

super在单继承中使用的例子:

class Foo():    def __init__(self, frob, frotz)        self.frobnicate = frob        self.frotz = frotzclass Bar(Foo):    def __init__(self, frob, frizzle)        super().__init__(frob, 34)        self.frazzle = frizzle

此例子适合python 3.x,更多请参考super

如果要在python2.x下使用则需要稍作调整,如下代码示例:

class Foo(object):     def __init__(self, frob, frotz):         self.frobnicate = frob         self.frotz = frotz class Bar(Foo):     def __init__(self, frob, frizzle):         super(Bar,self).__init__(frob,34)         self.frazzle = frizzle new = Bar("hello","world") print new.frobnicate print new.frazzle print new.frotz 
0 0
原创粉丝点击