Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #16822

sending a variable to an imported module

Date 2011-12-08 12:28 +0100
From Bastien Semene <bsemene@cyanide-studio.com>
Subject sending a variable to an imported module
Newsgroups comp.lang.python
Message-ID <mailman.3407.1323344097.27778.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

sending a variable to an imported module Bastien Semene <bsemene@cyanide-studio.com> - 2011-12-08 12:28 +0100

csiph-web