<p>您不应该将<code>cls</code>传入<code>super().__call__()</code>;<code>super()</code>为您处理绑定,因此{<cd1>}已经被自动传入了<em>。在</p>
<p>您可能被<code>__new__</code>中的<code>super().__new__(cls, ...)</code>调用弄糊涂了,这是因为<code>__new__</code>是这里的例外,请参见<a href="https://docs.python.org/3/reference/datamodel.html#object.__new__" rel="nofollow noreferrer">^{<cd8>} documentation</a>:</p>
<blockquote>
<p>Called to create a new instance of class <code>cls. __new__()</code> 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.</p>
</blockquote>
<p>从<code>super().__call__(...)</code>表达式中删除<code>cls</code>起作用:</p>
<pre><code>class ListMetaclass(type):
def __new__(cls, name, bases, attrs):
attrs['add'] = lambda self, value: self.append(value)
new_cls = type.__new__(cls, name, bases, attrs)
return new_cls;
def __call__(cls, *args, **kw):
return super().__call__(*args, **kw)
</code></pre>
<p>通过传入<code>cls</code>,实际上是在执行<code>list(cls)</code>,告诉<code>list()</code>将{<cd1>}转换为新列表中的值;这要求{<cd1>}为iterable。在</p>
<p>当您移除元类上的<code>__call__</code>方法时,当您调用<code>MyClass()</code>时,将使用默认的<code>type.__call__</code>方法,该方法只接收常规参数(示例中没有)并返回一个新实例。在</p>