Originariamente volevo chiedere this question, ma poi ho scoperto che era già pensato prima ...Python allungabile con - con super() Python 3 vs Python 2
Googling in giro ho trovato questo esempio di extending configparser. Le seguenti opere con 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()
Ma non con Python 2:
>>> class AmritaConfigParser(SafeConfigParser):
... def __init__(self):
... super(SafeConfigParser).init()
...
>>> cfg = AmritaConfigParser()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in __init__
TypeError: must be type, not classob
poi ho letto un po 'su Python nuova classe vs. stili vecchia classe (ad es here e ora ho. mi chiedo, non posso fare:
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"""
Ma, non dovrei chiamare init è questo in Python 2 l'equivalente:?
012.351.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)
Nel tuo esempio non è necessario definire un '__init __()' nella sottoclasse se tutto ciò che fa è chiamare la classe super '__init __()' (in o Python 2 o 3) - invece lascia che i super vengano ereditati. – martineau
Riferimento utile: http://amyboyle.ninja/Python-Inheritance/ –