擅长:python、mysql、java
<p>TL;DR-如果您尝试从元类<code>__new__</code>或<code>__init__</code>方法使用对<code>super</code>的空调用,您确实会得到<code>RuntimeError: super(): empty __class__ cell...</code>:在这个阶段,<code>super</code>内部使用的隐式“magic”变量<code>__class__</code>尚未创建。(在验证这一点时,我发现Python3.6中已经修复了这个问题——也就是说,在Python3.6中,使用无参数<code>super</code>的类方法可以从元类的<code>__init__</code>中调用,但在3.5中会产生这个错误)</p>
<p>如果这是您目前唯一遇到的问题,只需硬编码对超类方法的调用,就像在Python中创建<code>super</code>之前所需要的那样。(使用<code>super</code>的冗长形式将不起作用)。在</p>
<p>在</p>
<p>使用classmethods作为注册的类装饰器,可以通过使用一个简单的Python名称修改来自动创建每个manager类唯一的<code>SubManagers</code>属性来实现,方法是检查一个类在其<code>__dict__</code>中自己的名称空间,从而自动创建每个管理器类唯一的<code>SubManagers</code>属性也可以不用元类)</p>
<p>使用元类,只需在元类的末尾添加以下两行<code>__init__</code>:</p>
<pre><code>if getattr(cls, "SubManagers") and not "SubManagers" in cls.__dict__:
cls.SubManagers = []
</code></pre>
<p>如果您的类修饰符方法排除了元类,则不需要为此使用元类-更改register方法以执行上述“own”子管理器列表创建:</p>
^{pr2}$