splitting common functions into a sepperate module

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Thu Mar 8 04:06:39 EST 2007


En Thu, 08 Mar 2007 05:45:24 -0300, <jonkersbart at gmail.com> escribió:

> 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. Python uses different namespaces for
> different modules so I can't access the variables of the script in the
> module.

Are those *really* global variables? If yes, you could put them in yet  
another module, maybe config.py, and always get/set them using:
import config
config.xxx = "abc"
foo(x, config.yyy)
(do *not* use: from config import xxx)

Or, you may consider placing the global variables on the same module as  
those common functions (if that make some sense...)

But perhaps they are not really global variables, and you can use some  
class to hold them, specially if you can logically attach some methods to  
it.

Without further knowledge of the application, that's all I can say.

-- 
Gabriel Genellina




More information about the Python-list mailing list