Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!txtfeed1.tudelft.nl!tudelft.nl!txtfeed2.tudelft.nl!amsnews11.chello.com!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.01; 'beginner': 0.04; 'subject:module': 0.04; 'test,': 0.04; 'parameter': 0.05; 'attribute': 0.07; '===': 0.09; '[],': 0.09; 'doc,': 0.09; 'global,': 0.09; 'nameerror:': 0.09; 'run.': 0.09; 'top-level': 0.09; 'output': 0.10; 'am,': 0.12; 'def': 0.13; 'class,': 0.15; '-1)': 0.16; 'debates': 0.16; 'did,': 0.16; 'object).': 0.16; 'obvious.': 0.16; 'singleton': 0.16; 'subject:sending': 0.16; 'subject:variable': 0.16; '\xe9crit': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.18; 'yet.': 0.18; 'cc:no real name:2**0': 0.20; 'trying': 0.21; 'maybe': 0.21; "doesn't": 0.22; 'header:In-Reply-To:1': 0.22; 'module,': 0.23; 'values.': 0.23; 'defined': 0.24; 'cc:2**0': 0.24; 'keyword': 0.24; 'code': 0.25; 'module': 0.26; 'load': 0.26; "i'm": 0.26; 'import': 0.27; 'code,': 0.27; 'variable': 0.28; 'putting': 0.28; '(this': 0.28; 'script': 0.28; 'pass': 0.29; 'forgot': 0.29; 'module.': 0.29; "skip:' 10": 0.29; 'print': 0.29; 'cc:addr:python.org': 0.29; 'lines': 0.30; 'imported': 0.30; 'threads': 0.30; 'thanks': 0.31; "i've": 0.31; 'values': 0.32; 'value.': 0.32; 'pretty': 0.32; "can't": 0.32; "isn't": 0.33; 'sort': 0.33; 'header:User-Agent:1': 0.33; 'there': 0.33; 'object': 0.33; 'probably': 0.34; 'handled': 0.34; 'something': 0.35; 'totally': 0.35; 'test': 0.35; 'thread': 0.37; 'variables': 0.37; 'skip:" 10': 0.37; 'two': 0.37; 'but': 0.37; 'list,': 0.37; 'another': 0.37; 'think': 0.37; 'skip:_ 10': 0.37; 'enough': 0.38; 'using': 0.38; 'created': 0.38; 'put': 0.38; "i'd": 0.39; 'should': 0.39; "couldn't": 0.39; 'define': 0.39; 'why': 0.39; 'missing': 0.40; 'within': 0.60; 'achieve': 0.61; 'your': 0.61; 'internet': 0.64; 'ways,': 0.67; 'supply': 0.69; 'wish': 0.70; 'relevant': 0.70; 'soon': 0.70; 'anything.': 0.71; 'anytime': 0.74; "'test'": 0.84; 'returns.': 0.84; 'following.': 0.91; 'retype': 0.91; 'to:none': 0.93 Date: Thu, 08 Dec 2011 14:47:00 +0100 From: Bastien Semene User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:8.0) Gecko/20111105 Thunderbird/8.0 MIME-Version: 1.0 CC: python-list@python.org Subject: Re: sending a variable to an imported module References: <4EE09F55.2050504@cyanide-studio.com> <4EE0AA5E.8000005@davea.name> In-Reply-To: <4EE0AA5E.8000005@davea.name> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 80 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1323352016 news.xs4all.nl 6970 [2001:888:2000:d::a6]:38343 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:16826 Thanks both, Putting the variable inside a module works well. As the content is an object created inside another module I'm using this trick : module.CONFIG = module.load() So the variable is handled by the module that creates/use it, easy to use and pretty "native" to understand. Le 08/12/2011 13:15, Dave Angel a écrit : > On 12/08/2011 06:28 AM, Bastien Semene wrote: >> 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 >> >> > Please paste your code and your stacktrace, don't retype them. In the > above, you spelled 'imported_module" two different ways, and forgot > the quotes around "modue", so it couldn't run. There are probably > other problems, but what's the point? >> >> 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. > global variables are global only within their own module, but you > probably knew that. > > And using the global keyword in main.py isn't accomplishing anything. > Since you're not inside a def or a class, test is already global, as > soon as you give it a value. > > You don't pass values to a module, you load the module. And if the > module doesn't have any top-level code, you can "monkey-patch" it to > your heart's content, on lines following. > > If mymodule.py doesn't have a global value test, and you wish it did, > you can simply do something like: > import mymodule > mymodule.test = 42 > This attribute of mymodule is totally unrelated to one of the same > name in main.py. if you want to refer to it, or to change it again, > from main.py, you'd have to use mymodule.test. > > If the module had top-level code that needed to see your new global, > then you'd have a problem, because you can't put it there till after > the import returns. > > Now, most of the time when this sort of thing happens, what you really > want is to define another module whose only purpose is to supply these > common values. That module should get imported by both your script > and your module. > >