Tim*_*zza 1 python initialization class subclass enumerable
我在一个项目中使用init_subclass,当我遇到内置方法时,当代码首次在解释器中运行时启动时,我有点犹豫——没有通过包含类或它的子类的实例化直接引用列举。
有人可以告诉我发生了什么,并指出安全使用的任何示例吗?
class Timer():
def __init__(self):
pass
def __init_subclass__(cls):
print('Runner.', cls)
print('Timer Dictionary :', Timer.__dict__.keys())
# print(Timer.__init_subclass__()) # Forbidden fruit...
pass
class Event(Timer):
print("I'll take my own bathroom selfies...thanks anyway.")
def __init__(self):
print('This is nice, meeting on a real date.')
if __name__ == '__main__': # a good place for a breakpoint
date = Event()
date
Run Code Online (Sandbox Code Playgroud)
编辑 - - - - - - - - - - - - - - - - - - - - - - - - - ——
根据收到的解释,原始代码被改造成有用的东西。
class Timer():
subclasses = {}
def __init__(self):
pass
def __init_subclass__(cls, **kwargs):
print('Runner.', cls)
print('Timer Dictionary :', Timer.__dict__.keys())
# print(Timer.__init_subclass__()) # Forbidden fruit...
super().__init_subclass__(**kwargs)
cls.subclasses[cls] = []
class Event(Timer):
print("I'll take my own bathroom selfies...thanks anyway.")
def __init__(self):
print('This is nice, meeting on a real date.')
if self.__class__ in super().subclasses:
# get the index and link the two
super().subclasses[self.__class__].append(self)
if __name__ == '__main__': # a good place for a breakpoint
date = Event()
date
duty = Event()
duty
print(Timer.subclasses)
Run Code Online (Sandbox Code Playgroud)
这是一个最小的例子:
class Super():
def __init_subclass__(cls):
print(cls)
class Sub(Super):
pass
Run Code Online (Sandbox Code Playgroud)
运行这个:
$ python test.py
<class '__main__.Sub'>
Run Code Online (Sandbox Code Playgroud)
这是为什么?根据Python 的数据模型文档:
每当一个类从另一个类继承时,就会在该类上调用init_subclass。
Sub继承自Super,因此Super.__init_subclass__()被调用。
具体来说,在实现中type_new() 调用 init_subclasscpython。
PEP 487 中详细说明了基本原理。