擅长:python、mysql、java
<p>为了理解你为什么会得到结果,你必须理解元类是什么。要理解这一点,您必须了解如何在Python中创建类。在</p>
<p>当您编写<code>class</code>语句时,解释器会打开一个新的名称空间,执行语句中的代码,并在代码运行后查看名称空间的状态。然后计算出重要的细节(局部变量、类的名称,…)并将其传递给内置类<code>type</code>。因此,<code>type</code>的实例就是我们通常所说的类,因此<code>type</code>是一个“元类”。在</p>
<p>重写元类时,告诉Python不要使用<code>type</code>来创建新类。相反,它将把相同的东西传递给一个自定义类。这允许您覆盖<code>type</code>中的默认值。在</p>
<p>因此,<code>person.Person</code>是通过向元类传递一些参数来创建的。特别是,它是该类的一个实例,因此它的<code>__class__</code>就是元类!在</p>