【问题标题】:How can I call super() so it's compatible in 2 and 3?如何调用 super() 使其在 2 和 3 中兼容?
【发布时间】:2014-03-13 04:00:02
【问题描述】:

我正在尝试使用 six 编写 2/3 兼容代码,但我不知道如何以交叉兼容的方式调用 super()。除此之外还有没有更好的方法,例如:

class MyClass(MyBase):
    def __init__():
        if six.PY3:
            super().__init__()
        else:
            super(MyClass, self).__init__()
        ...

【问题讨论】:

    标签: python six-python


    【解决方案1】:

    super() 与参数一起使用是向后兼容的,因此您应该能够使用super(MyClass, self) 而无需检查版本。

    【讨论】:

      猜你喜欢
      • 2019-02-07
      • 1970-01-01
      • 2017-08-13
      • 2021-07-27
      • 1970-01-01
      • 1970-01-01
      • 2013-05-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多