Python 扩展 - 使用 super() Python 3 vs Python 2 [英] Python extending with - using super() Python 3 vs Python 2

查看:22
本文介绍了Python 扩展 - 使用 super() Python 3 vs Python 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本来我想问这个问题,但后来发现已经是之前想过...

谷歌搜索我发现了这个例子 扩展配置解析器.以下适用于 Python 3:

$ python3Python 3.2.3rc2(默认,2012 年 3 月 21 日,06:59:51)[GCC 4.6.3] 在 linux2 上>>>从 configparser 导入 SafeConfigParser>>>类 AmritaConfigParser(SafeConfigParser):... def __init__(self):... super().__init__()...>>>cfg = AmritaConfigParser()

但不适用于 Python 2:

<预><代码>>>>类 AmritaConfigParser(SafeConfigParser):... def __init__(self):... super(SafeConfigParser).init()...>>>cfg = AmritaConfigParser()回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中.文件

然后我阅读了一些关于 Python 新类与旧类样式的内容(例如 此处.现在我想知道,我可以这样做:

class MyConfigParser(ConfigParser.ConfigParser):def 写(自我,fp):"覆盖模块的原始写入功能""....def MyWrite(self, fp):"定义新函数并继承所有其他函数""

但是,我不应该调用 init 吗?这在 Python 2 中是否等效:

 类 AmritaConfigParser(ConfigParser.SafeConfigParser):#def __init__(self):# super().__init__() # Python3 语法,或者更确切地说,新风格的类语法 ...## 这和上面的一样吗?def __init__(self):ConfigParser.SafeConfigParser.__init__(self)

解决方案

  • super()(不带参数)是在 Python 3 中引入的(与 __class__ 一起):

    super() ->与 super(__class__, self) 相同

    所以这将是新式类的 Python 2 等价物:

    super(CurrentClass, self)

  • 对于老式的类,你总是可以使用:

     class Classname(OldStyleParent):def __init__(self, *args, **kwargs):OldStyleParent.__init__(self, *args, **kwargs)

Originally I wanted to ask this question, but then I found it was already thought of before...

Googling around I found this example of extending configparser. The following works with 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()

But not with 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

Then I read a little bit on Python New Class vs. Old Class styles (e.g. here. And now I am wondering, I can do:

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"""

But, shouldn't I call init? Is this in Python 2 the equivalent:

 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() (without arguments) was introduced in Python 3 (along with __class__):

    super() -> same as super(__class__, self)
    

    so that would be the Python 2 equivalent for new-style classes:

    super(CurrentClass, self)
    

  • for old-style classes you can always use:

     class Classname(OldStyleParent):
        def __init__(self, *args, **kwargs):
            OldStyleParent.__init__(self, *args, **kwargs)
    

这篇关于Python 扩展 - 使用 super() Python 3 vs Python 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆