splitting common functions into a sepperate module

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Thu Mar 8 10:36:47 CET 2007

jonkersbart at gmail.com a écrit :
> Dear,
> I have wrote a script and want to group some functions of the script
> in a separate modulo so that I can import the module in other scripts
> and use the same functions there..
> The problem is that the common functions need access to some global
> variables defined in the script.

And now you find out why globals are bad...

> Python uses different namespaces for
> different modules so I can't access the variables of the script in the
> module.
> What is the best solution to solve this problem?

There's no one-size-fits-all answer. Usual solutions include:
- passing the needed values as args to the functions
- wrapping the functions as methods of a class, passing the whole state 
as args to the class initializer.

More information about the Python-list mailing list