Initializing a python module with parameters

Gerhard Häring gerhard.haering at opus-gmbh.net
Wed Dec 4 12:46:49 EST 2002


Iggeres Bet <iggeres at yahoo.es> wrote:

> Hello,
> 
> I am writing a Python module using the C API, but I need to call the
> module
> init...() with custom parameters to initialize it on different
> environments.
> 
> The perfect solution would be a:
> 
>    # Calling a module constructor
>    import mymodule(<parameters>)
> 
> I can write:
>    import mymodule
>    mymodule.init(<parameters>)
> 
> But I don't see this code explicit and elegant. What is the best way
> to pass initialization parameters to a Python module?

There is no way to directly pass parameters to a Python module.

What about creating a function that creates a singleton object having static
methods instead?  This way, you can pass parameters to the function.

Otherwise, the module would need to get its parameters from something external
to it, like from another module:

#v+
import configuration

def fooUnix:
    # ...

def fooWindows:
    # ...

if configuration.getPlatform() == configuration.UNIX:
    foo = fooUnix
else:
    foo = fooWindows

__all__ = ["foo"]
#v-

Silly example, but you get the idea.
-- 
Gerhard Häring
OPUS GmbH München
Tel.: +49 89 - 889 49 7 - 32
http://www.opus-gmbh.net/



More information about the Python-list mailing list