我在一个项目中使用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)
编辑 - - - - - - - - - - - - - - - - - - - - - - …