Python расширяется с использованием super () Python 3 против Python 2
Изначально я хотел спроситьэтот вопрос, но потом я обнаружил, что об этом уже думали раньше ...
Погуглив, я нашел этот примеррасширяющий configparser, Следующие работы с 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>
Но не с 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>
Затем я немного прочитал о стилях Python New Class и Old Class (например,Вот. And now I am wondering, I can do:
<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>
Но разве я не должен вызывать init? Это в Python 2 эквивалентно:
<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>