r/pythontips • u/DoomLimpio • Feb 29 '24
Meta code in __init__.py
Hi there!
Recently, I've started working in a new team, and they have several practices that, I won't say are wrong, but seem a bit odd to me. One thing that caught my attention is how they're creating classes/functions inside __init__.py files. In my experience, those files are usually kept empty or just handle basic imports, acting more like an interface when you import the package.
What are your thoughts on this?
13
Upvotes
View all comments
17
u/[deleted] Feb 29 '24
The init file can contain a code, it is executed when (or right before) the package/module is imported. So if one want's to run some code before the import happens e.g. for some setups, it is a good place for it.