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)
感謝您提前幫助我清理事情。
總結
以上是生活随笔為你收集整理的super在python3和python2_Python扩展与 – 使用super()python 3 vs python 2的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 提取C3D视频特征(官方文档&实
- 下一篇: python初学篇笔记_Python学习