Instatiating module / Reusing module of command-line tool

Loris Bennett loris.bennett at fu-berlin.de
Fri May 6 08:11:24 EDT 2022


ram at zedat.fu-berlin.de (Stefan Ram) writes:

> "Loris Bennett" <loris.bennett at fu-berlin.de> writes:
>>My question: What is the analogue to initialising an object via the
>>constructor for a module?
>
>   If you need a class, you can write a class.
>
>   When one imports a module, the module actually gets executed.
>   That's why people write "if __name__ == '__main__':" often.
>   So, everything one wants to be done at import time can be 
>   written directly into the body of one's module.

So if I have a module which relies on having internal data being set
from outside, then, even though the program only ever has one instance
of the module, different runs, say test and production, would require
different internal data and thus different instances.  Therefore a class
seems more appropriate and it is more obvious to me how to initialise
the objects (e.g. by having the some main function which can read
command-line arguments and then just pass the arguments to the
constructor.

I suppose that the decisive aspect is that my module needs
initialisation and thus should to be a class.  Your examples in the
other posting of the modules 'math' and 'string' are different, because
they just contain functions and no data.

Cheers,

Loris

-- 
This signature is currently under construction.


More information about the Python-list mailing list