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%

asked Apr 24, 2014 at 13:30
3
  • Can you give us more information? In what circumtances? Can you provide an example? Commented Apr 24, 2014 at 13:33
  • What is it about the linked answer that you feel doesn't answer your problem? Commented Apr 24, 2014 at 13:35
  • The purpose of the initialize function is to initialize the global variables of the module. Commented Apr 24, 2014 at 13:55

2 Answers 2

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.

answered Apr 24, 2014 at 13:32
Sign up to request clarification or add additional context in comments.

Comments

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.

answered Jun 8, 2023 at 15:22

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.