小编NSD*_*ont的帖子

为什么__new__方法在超级方法中调用其父级__new__时需要传递cls参数?

我知道当我们通过super方法调用parent的方法时,我们可以忽略绑定方法中的"self"参数,如下所示:

class Foo(object):
    def __init__(self):
        super(Foo, self).__init__() # We needn't pass in the "self" argument
        # ...
Run Code Online (Sandbox Code Playgroud)

__new__方法有所不同:

class Bar(object):
    def __new__(cls, *args, **kwargs):
        return super(Bar, cls).__new__(cls, *args, **kwargs) # Why need a "cls" argument?
Run Code Online (Sandbox Code Playgroud)

python super

1
推荐指数
1
解决办法
126
查看次数

标签 统计

python ×1

super ×1