sending a variable to an imported module

B

Bastien Semene

Hi list,

I'm trying to pass a variable to an imported module without singletons.
I've seen in the doc, and tested that I can't use global to do it :

=== module.py ===
def testf():
print test


=== main.py ===
global test
test = 1

imported_module = __import__(module, globals(), locals(), [], -1)

importmodule.testf()

=== output ===
NameError: global name 'test' is not defined



While I was reading many (many) threads about singleton I read people
claiming that singletons can always be avoided (I can't remeber the most
relevant thread on stackoverflow).
I don't want to start a new debate about singletons, I think Internet
has enough debates yet.

But in my case I'd like to access this variable anywhere and at anytime
without having to pass it as a parameter everywhere (this variable is a
configuration manager object).
How can I achieve that without singletons ?
I'm beginner in Python, that's why I'm maybe missing something obvious.

Thanks,
Bastien Semene
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top