3 ms·
As a personal opinion, I always found adding code into a "__init__" file a fudge and an antipatern.
by Octabrain 4y ago
As a personal opinion, I always found adding code into a "__init__" file a fudge and an antipatern.
- OJFord 4y agoI agree beyond imports (it can be convenient to allow e.g. from django.db.models import CharField instead of from django.db.models.fields or whatever it is, for example) - that's why I called it laziness, a fudge done to just get it working quickly, lazily not doing it properly.
- BerislavLopac 4y agoNot at all - it is an excellent way to make modules, well, modular - allowing complex internal namespacing while keeping the surface clean and simple.
- Octabrain 4y ago> it is an excellent way to make modules, well, modular Strongly disagree. It is perfectly possible to do that without adding anything into __init__. I prefer explicit imports and any other logic declared directly into the module(s) that are the places where one would expect to find those. I see code in __init__ as a convenient hack, but a hack nonetheless.
- BerislavLopac 4y agoCode in __init__ is one of the core concepts of Python; seeing it as a hack is purely individual, and probably based on your own experience with it. It's exactly the same as calling mataclasses "a hack".
- Octabrain 4y ago> Code in __init__ is one of the core concepts of Python; seeing it as a hack is purely individual Yes, in fact I started my first message with a clear "As a personal opinion...". Apart from that, the comparison with metaclasses does not make sense. Metaclasses are an OOP concept.