Skip to content
Advertisement

Better place to put common functions?

I’m working on building a custom package for functions I commonly use, and it has several functions that do not fit in any specific module and are used by several modules. I’ve been putting them in __init__.py, and it works, but I’ve seen many tutorials that recommend a very small __init__.py. Is there any better place I can put them?

I want to be able to call them like this:

JavaScript

Where do I put these functions?

Advertisement

Answer

You could create a ‘utilities’ package/file for storing re-usable pieces of code and then import them into your files which could benefit from them !

And as for your init.py file – I’d leave that empty !

JavaScript

If you add the parent folder to the python path, within the command_line_interface.py or data_access.py file you can import

from extras.utilities import *

and you will have access to all your re-usable methods

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement