super在python3和python2_Python扩展与 – 使用super()python 3 vs python 2

本来我想问

this question,但后来我发现它已经被想到了之前…

Googling我发现这个例子extending configparser.下面的工作与python 3:

$ python3

Python 3.2.3rc2 (default, Mar 21 2012, 06:59:51)

[GCC 4.6.3] on linux2

>>> from configparser import SafeConfigParser

>>> class AmritaConfigParser(SafeConfigParser):

... def __init_(self):

... super().__init__()

...

>>> cfg = AmritaConfigParser()

但不是用python2:

>>> class AmritaConfigParser(SafeConfigParser):

... def __init__(self):

... super(SafeConfigParser).init()

...

>>> cfg = AmritaConfigParser()

Traceback (most recent call last):

File "", line 1, in

File "", line 3, in __init__

TypeError: must be type, not classob

然后我读了一些关于Python新类与旧类样式(例如here。

现在我想知道,我可以做:

class MyConfigParser(ConfigParser.ConfigParser):

def Write(self, fp):

"""override the module's original write funcition"""

....

def MyWrite(self, fp):

"""Define new function and inherit all others"""

但是,我不应该调用init?这是在python 2等效:

class AmritaConfigParser(ConfigParser.SafeConfigParser):

#def __init__(self):

# super().__init__() # Python3 syntax, or rather, new style class syntax ...

#

# is this the equivalent of the above ?

def __init__(self):

ConfigParser.SafeConfigParser.__init__(self)

感谢您提前帮助我清理事情。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值