1. <legend id='c3lMa'><style id='c3lMa'><dir id='c3lMa'><q id='c3lMa'></q></dir></style></legend>

      <small id='c3lMa'></small><noframes id='c3lMa'>

      <i id='c3lMa'><tr id='c3lMa'><dt id='c3lMa'><q id='c3lMa'><span id='c3lMa'><b id='c3lMa'><form id='c3lMa'><ins id='c3lMa'></ins><ul id='c3lMa'></ul><sub id='c3lMa'></sub></form><legend id='c3lMa'></legend><bdo id='c3lMa'><pre id='c3lMa'><center id='c3lMa'></center></pre></bdo></b><th id='c3lMa'></th></span></q></dt></tr></i><div id='c3lMa'><tfoot id='c3lMa'></tfoot><dl id='c3lMa'><fieldset id='c3lMa'></fieldset></dl></div>
      <tfoot id='c3lMa'></tfoot>

      • <bdo id='c3lMa'></bdo><ul id='c3lMa'></ul>
    2. 使用多个 __init__ 参数子类化元组

      Subclassing tuple with multiple __init__ arguments(使用多个 __init__ 参数子类化元组)

      1. <small id='1DPur'></small><noframes id='1DPur'>

          <tbody id='1DPur'></tbody>

        <tfoot id='1DPur'></tfoot>
        <legend id='1DPur'><style id='1DPur'><dir id='1DPur'><q id='1DPur'></q></dir></style></legend>
          <i id='1DPur'><tr id='1DPur'><dt id='1DPur'><q id='1DPur'><span id='1DPur'><b id='1DPur'><form id='1DPur'><ins id='1DPur'></ins><ul id='1DPur'></ul><sub id='1DPur'></sub></form><legend id='1DPur'></legend><bdo id='1DPur'><pre id='1DPur'><center id='1DPur'></center></pre></bdo></b><th id='1DPur'></th></span></q></dt></tr></i><div id='1DPur'><tfoot id='1DPur'></tfoot><dl id='1DPur'><fieldset id='1DPur'></fieldset></dl></div>
              • <bdo id='1DPur'></bdo><ul id='1DPur'></ul>

                本文介绍了使用多个 __init__ 参数子类化元组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                以下代码有效:

                class Foo(tuple):
                
                    def __init__(self, b):
                        super(Foo, self).__init__(tuple(b))
                
                if __name__ == '__main__':
                    print Foo([3, 4])
                
                $ python play.py 
                

                结果:

                play.py:4: DeprecationWarning: object.__init__() takes no parameters
                  super(Foo, self).__init__(tuple(b))
                (3, 4)
                

                但不是以下:

                class Foo(tuple):
                
                    def __init__(self, a, b):
                        super(Foo, self).__init__(tuple(b))
                
                if __name__ == '__main__':
                    print Foo(None, [3, 4])
                
                $ python play.py 
                

                结果:

                Traceback (most recent call last):
                  File "play.py", line 7, in <module>
                    print Foo(None, [3, 4])
                TypeError: tuple() takes at most 1 argument (2 given)
                

                为什么?

                推荐答案

                因为元组是不可变的,所以你必须重写 __new__:

                Because tuples are immutable, you have to override __new__ instead:

                python 文档

                object.__new__(cls[, ...])

                调用创建一个新的实例类 cls.__new__() 是一个静态的方法(特殊情况,所以你不需要声明它这样),这需要实例所在的类请求作为它的第一个参数.这剩下的参数是那些通过的到对象构造函数表达式(对班级的呼吁).回报__new__() 的值应该是新的对象实例(通常是一个实例cls).

                Called to create a new instance of class cls. __new__() is a static method (special-cased so you need not declare it as such) that takes the class of which an instance was requested as its first argument. The remaining arguments are those passed to the object constructor expression (the call to the class). The return value of __new__() should be the new object instance (usually an instance of cls).

                典型的实现创建一个新的通过调用类的实例超类的 __new__() 方法使用super(currentclass, cls).__new__(cls[, ...]) 带有适当的参数和然后修改新创建的在返回之前根据需要实例它.

                Typical implementations create a new instance of the class by invoking the superclass’s __new__() method using super(currentclass, cls).__new__(cls[, ...]) with appropriate arguments and then modifying the newly-created instance as necessary before returning it.

                如果 __new__() 返回一个实例cls,然后是新实例的__init__() 方法将像 __init__(self[, ...]) 一样被调用,其中 self 是新实例,其余的参数与传递的相同到 __new__().

                If __new__() returns an instance of cls, then the new instance’s __init__() method will be invoked like __init__(self[, ...]), where self is the new instance and the remaining arguments are the same as were passed to __new__().

                如果 __new__() 不返回cls 的实例,然后是新的实例的 __init__() 方法不会被调用.

                If __new__() does not return an instance of cls, then the new instance’s __init__() method will not be invoked.

                __new__() 主要是为了允许不可变类型的子类(比如intstrtuple) 来自定义实例创建.这也很常见在自定义元类中被覆盖为了自定义类创建.

                __new__() is intended mainly to allow subclasses of immutable types (like int, str, or tuple) to customize instance creation. It is also commonly overridden in custom metaclasses in order to customize class creation.

                这篇关于使用多个 __init__ 参数子类化元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                How to extend Python class init(如何扩展 Python 类初始化)
                What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                Initialize list with same bool value(使用相同的布尔值初始化列表)
                setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)

                  <tbody id='BzddM'></tbody>

                    <small id='BzddM'></small><noframes id='BzddM'>

                    <legend id='BzddM'><style id='BzddM'><dir id='BzddM'><q id='BzddM'></q></dir></style></legend>
                  • <tfoot id='BzddM'></tfoot>

                      • <bdo id='BzddM'></bdo><ul id='BzddM'></ul>
                      • <i id='BzddM'><tr id='BzddM'><dt id='BzddM'><q id='BzddM'><span id='BzddM'><b id='BzddM'><form id='BzddM'><ins id='BzddM'></ins><ul id='BzddM'></ul><sub id='BzddM'></sub></form><legend id='BzddM'></legend><bdo id='BzddM'><pre id='BzddM'><center id='BzddM'></center></pre></bdo></b><th id='BzddM'></th></span></q></dt></tr></i><div id='BzddM'><tfoot id='BzddM'></tfoot><dl id='BzddM'><fieldset id='BzddM'></fieldset></dl></div>