Code Explanation:
1. Creating a Registry List
registry = []
An empty list named registry is created.
It will store the names of all subclasses of Plugin.
2. Defining the Base Class
class Plugin:
A base class named Plugin is defined.
It will automatically track all its subclasses.
3. Overriding __init_subclass__
def __init_subclass__(cls):
registry.append(cls.__name__)
__init_subclass__ is a special hook called every time a subclass is created.
cls refers to the newly created subclass.
The subclass name is appended to the registry list.
4. Creating Subclass A
class A(Plugin): pass
A is created as a subclass of Plugin.
This triggers:
Plugin.__init_subclass__(A)
"A" is appended to registry.
5. Creating Subclass B
class B(Plugin): pass
B is created as a subclass of Plugin.
This triggers:
Plugin.__init_subclass__(B)
"B" is appended to registry.
6. Printing the Registry
print(registry)
Prints the contents of registry.
7. Final Output
['A', 'B']
Final Answer
✔ Output:
['A', 'B']


0 Comments:
Post a Comment