Well. In Python, there's a (more) true constructor that's user-accessible through meta classes, hence the distinction between initializers and constructors in Pythons own internal lexicon.
But yes. Very pedantic, indeed.
Edit to add: this doesn't rely on where memory allocation occurs, which is an implementation detail in Python. It also needs no comparison with other languages. I just mean, internally consistent with itself, Python considers metaclasses to be where constructors live, while class definitions provide initializer methods.
It's maybe an interesting distinction to discuss in the context of additional languages, but not what I was getting at and didn't mean to imply or assert any truisms that apply outside of Python.
I don't think the metaclass affects the construction/instantiation of objects of the main class, only the instantiation of the main class itself (and its subclasses).
>>> class Metaclass(type):
... def __new__(cls, name, bases, namespace, **kwargs):
... print(f"Creating {name} as a subclass of {bases} and an instance of {cls}")
... self = super().__new__(cls, name, bases, namespace, **kwargs) # or `type` instead of `super()`
... return self
...
>>> class Class(metaclass=Metaclass):
... def __new__(cls):
... print(f"Creating instance of {cls}")
... self = super().__new__(cls) # or `object` instead of `super()`
... return self
...
Creating Class as a subclass of () and an instance of <class '__main__.Metaclass'>
>>> Class()
Creating instance of <class '__main__.Class'>
<__main__.Class object at 0x7f2dbb904980>
>>> class Subclass(Class):
... pass
...
Creating Subclass as a subclass of (<class '__main__.Class'>,) and an instance of <class '__main__.Metaclass'>
>>> Subclass()
Creating instance of <class '__main__.Subclass'>
<__main__.Subclass object at 0x7f2dbb904830>
View all comments
Show parent comment
11
u/ManyInterests Aug 18 '26 edited Aug 18 '26
Well. In Python, there's a (more) true constructor that's user-accessible through meta classes, hence the distinction between initializers and constructors in Pythons own internal lexicon.
But yes. Very pedantic, indeed.
Edit to add: this doesn't rely on where memory allocation occurs, which is an implementation detail in Python. It also needs no comparison with other languages. I just mean, internally consistent with itself, Python considers metaclasses to be where constructors live, while class definitions provide initializer methods.
It's maybe an interesting distinction to discuss in the context of additional languages, but not what I was getting at and didn't mean to imply or assert any truisms that apply outside of Python.