我想了解在Python中声明从父类继承的新类时实际发生了什么。
下面是一个非常简单的代码片段:
这是一段有明显错误的脚本,我继承了
这段代码可以正常运行,但我不明白为什么会调用
第8行,我使用没有参数的方式实例化了
我大致上能理解发生了什么:
下面是一个非常简单的代码片段:
# inheritance.py
class Foo():
def __init__(self, *args, **kwargs):
print("Inside foo.__init__")
print(args)
print(kwargs)
class Bar(Foo):
pass
print("complete")
如果我运行这个程序,没有错误,并且输出结果跟我预期的一样。
❯ python inheritance.py
complete
这是一段有明显错误的脚本,我继承了
Foo()
的一个实例而不是Foo
类。# inheritance.py
class Foo():
def __init__(self, *args, **kwargs):
print("Inside foo.__init__")
print(f"{args=}")
print(f"{kwargs=}\n")
foo = Foo()
class Bar(foo): <---- This is wrong
pass
print("complete")
这段代码可以正常运行,但我不明白为什么会调用
Foo.__init__()
两次。
以下是输出内容:❯ python inheritance.py
Inside foo.__init__ <--- this one I expected
args=()
kwargs={}
Inside foo.__init__ <--- What is going on here...?
args=('Bar', (<__main__.Foo object at 0x10f190b10>,), {'__module__': '__main__', '__qualname__': 'Bar'})
kwargs={}
complete
第8行,我使用没有参数的方式实例化了
Foo()
,这正是我所期望的。但是在第9行,调用了Foo.__init__
并传递了通常会传递给type()
以生成新类的参数。我大致上能理解发生了什么:
class Bar(...)
是生成一个新类的代码,因此在某个时候需要调用type("Bar", ...)
,但是:
- 这到底是如何发生的?
- 为什么从
Foo()
的实例进行继承会导致调用Foo.__init__("Bar", <tuple>, <dict>)
? - 为什么不直接调用
type("Bar", <tuple>, <dict>)
?
元类
而发生的事情。而不是使用Foo.__init__("Bar", <tuple>, <dict>)
,调用type(Foo)("Bar", <tuple>, <dict>)
。如果没有元类
,则type(Foo)
是type
。 - Axe319