Python se extiende con - usando super () Python 3 vs Python 2
Originalmente quise preguntaresta pregunta, pero luego descubrí que ya estaba pensado antes ...
Buscando en Google encontré este ejemplo deextendiendo configparser. Los siguientes trabajos con Python 3:
<code>$ 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() </code>
Pero no con Python 2:
<code>>>> 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 </code>
Luego leí un poco sobre los estilos Python New Class vs. Old Class (por ejemplo,aquí. Y ahora me pregunto, puedo hacer:
<code>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""" </code>
Pero, ¿no debería llamar a init? ¿Es esto en Python 2 el equivalente?
<code> 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) </code>