具有多个 __init__ 参数的子类化元组 [英] Subclassing tuple with multiple __init__ arguments

查看:20
本文介绍了具有多个 __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__ 参数的子类化元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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