4

When importing a custom module, I would like a given initialize() function to be run. The purpose of this function is mainly to initialize the global variables of the module.

Is calling the function inside mymodule.py frowned upon? Is it worth it to convert my module into a package and create a __init__.py file? Or should I just call the initialize() function from the main file that will import mymodule?

My question is somehow related to this one but the answers do not satisfy me 100%

Community
  • 1
  • 1
Niourf
  • 450
  • 1
  • 4
  • 15

2 Answers2

0

A package is only necessary if you have more than one module, to package together. If your initialize is somehow work intensive you could the importing program decide when and how to initialize, otherwise all initialize inside your module.

Daniel
  • 42,087
  • 4
  • 55
  • 81
0

When it comes to creating a module, you want those who will be using it to have the fewest steps to perform, and thus the least risk of error. So, creating a __init__.py that is capable of doing all the initialization itself is a good solution.

It's part of the very philosophy behind the existence of modules: those who use a module don't want to have to tune internal details which are of no use to them.

PiRob
  • 1
  • 1