Q71: What sequence occurs when executing MyClass(*args) under the hood?
When executing MyClass(*args):
- Invokes metaclass
__call__()(typicallytype.__call__). type.__call__invokesMyClass.__new__(MyClass, *args)to allocate the object.- If
__new__returns an instance ofMyClass, it invokesMyClass.__init__(instance, *args). - Returns the instance.
Key Takeaways
- Triggers metaclass
__call__execution. __new__allocates instance;__init__configures state.
Knowledge Check
What happens if __new__ returns an instance of a COMPLETELY DIFFERENT class?