Issue36178
Created on 2019-03-04 08:44 by Hameer Abbasi, last changed 2022-04-11 14:59 by admin. This issue is now closed.
| Messages (3) | |||
|---|---|---|---|
| msg337079 - (view) | Author: Hameer Abbasi (Hameer Abbasi) | Date: 2019-03-04 08:44 | |
I may be completely misunderstanding here, but: here's a reproducible example:
class MyMeta(type):
def __new__(cls, *args, **kwargs):
print('__new__', *args, **kwargs)
super().__new__(cls, *args, **kwargs)
def __init__(self, a):
print('__init__', *args, **kwargs)
super().__init__(*args, **kwargs)
class A(metaclass=MyMeta):
pass
MyMeta('A', (), {'__module__': '__main__', '__qualname__': 'A'})
Output:
__new__ A () {'__module__': '__main__', '__qualname__': 'A'}
__new__ A () {'__module__': '__main__', '__qualname__': 'A'}
Is this by design?
|
|||
| msg337085 - (view) | Author: Steven D'Aprano (steven.daprano) * ![]() |
Date: 2019-03-04 09:17 | |
Your metaclass.__new__ method returns None instead of the new class. The rule for calling __init__ is: - if the constructor __new__ returns an instance of the type, then call the initializer __init__ - otherwise, don't call __init__ at all. https://docs.python.org/3/reference/datamodel.html#object.__new__ Since your __new__ accidentally returns None, the __init__ is not called. If you change the line to say return super().__new__(cls, *args, **kwargs) you will see that __init__ is called. (And discover the bugs in your init method :-) |
|||
| msg337086 - (view) | Author: Hameer Abbasi (Hameer Abbasi) | Date: 2019-03-04 09:22 | |
Ah, I wasn't aware I had to return... The bug was deliberate to show that not even a different signature makes a difference. ;) |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022-04-11 14:59:11 | admin | set | github: 80359 |
| 2019-03-04 09:22:24 | Hameer Abbasi | set | messages: + msg337086 |
| 2019-03-04 09:17:57 | steven.daprano | set | status: open -> closed nosy:
+ steven.daprano resolution: not a bug |
| 2019-03-04 08:44:53 | Hameer Abbasi | create | |
