Code Explanation
class Meta(type):
def __init__(cls, name, bases, dct):
print("Initializing:", name)
Defines a custom metaclass Meta, which inherits from type.
__init__ is a special method that is called when a class is created.
Parameters of __init__:
cls → The class being created (e.g., A).
name → Name of the class being created ("A" in this case).
bases → Tuple of base classes (() since A has no parent class).
dct → Dictionary containing class attributes and methods.
The print() statement executes when the class is created, not when an object is instantiated.
class A(metaclass=Meta):
pass
Defines class A using Meta as its metaclass.
Since Meta is the metaclass, Python calls Meta.__init__ to initialize A.
The print() statement inside Meta.__init__ runs immediately.
Output:
Initializing: A
0 Comments:
Post a Comment