Tuesday, 15 June 2010

python - Is there a way to get importer's variable in importee? -



python - Is there a way to get importer's variable in importee? -

say have 2 python file

# importer.py parameter = 4 import importee

and

# importee.py print parameter

can or how can in importee.py access importer.py's parameter?

i'm using ugly work around, borrow (and pollute) sys

# importer.py import sys sys.parameter = 4 import importee

and

# importee.py print sys.parameter

too ugly. looking improve solution.

the recommended way accomplish think want accomplish declare function in importee, , phone call it, e.g.:

# importer.py import importee importee.call_me(4)

and:

# importee.py def call_me(parameter): print(parameter)

it preferable avoid performing operations in global scope. , print()ing suppose minimal illustration doesn't match real utilize case :).

by way, ugly work around have mentioned practically equivalent using separate configuration module. example:

# importer.py import config config.param = 4 import importee

+

# importee.py import config print(config.param)

+

# config.py param = 7 # default

it's still close pretty @ to the lowest degree avoids mangling scheme modules.

python

No comments:

Post a Comment