Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #16825
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <d@davea.name> |
| 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; 'received:192.168.1.104': 0.16; 'singleton': 0.16; 'subject:sending': 0.16; 'subject:variable': 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; '(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; "i've": 0.31; 'values': 0.32; 'value.': 0.32; "can't": 0.32; "isn't": 0.33; 'sort': 0.33; 'header:User-Agent:1': 0.33; 'there': 0.33; 'probably': 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; 'received:192': 0.37; 'another': 0.37; 'think': 0.37; 'skip:_ 10': 0.37; 'enough': 0.38; 'using': 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; 'received:192.168': 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; 'header:Reply-To:1': 0.71; 'anything.': 0.71; 'reply-to:no real name:2**0': 0.72; 'anytime': 0.74; "'test'": 0.84; 'returns.': 0.84; 'following.': 0.91; 'retype': 0.91 |
| Date | Thu, 08 Dec 2011 07:15:26 -0500 |
| From | Dave Angel <d@davea.name> |
| User-Agent | Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.23) Gecko/20110922 Thunderbird/3.1.15 |
| MIME-Version | 1.0 |
| To | Bastien Semene <bsemene@cyanide-studio.com> |
| Subject | Re: sending a variable to an imported module |
| References | <4EE09F55.2050504@cyanide-studio.com> |
| In-Reply-To | <4EE09F55.2050504@cyanide-studio.com> |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Provags-ID | V02:K0:SP1h/RW2xlp1dVfNwzunS8BwxadyBsgYxYhdMkYokZ6 v2r7Ot0IIVMRRGktgAhR7S16S8L3xPxYdE8XkS7C7z5jsVi2bQ uNHg5v0FXa/0XS4B87ZQuYLnxJwGSH3OsSpRi3zYrmkIGbE3wY HHzq1Lwrl1We43XZHtazBTUrOE3nG6kO7QbhwyoBYOvPx3GLPp 5oC1mE928SSSHCPUgk1BTfiCtrk8AHpCIX90sMuRXORR+YmZnZ NQkycuk5Kgqn/8zFot5A8yPVwEPIVfEkPlt4JOos1L28KS6BBU Y2CWPuP8BvGjyVO+l0GYbpNIGAu5945xWeofbfPMrBu48lZqg= = |
| Cc | python-list@python.org |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.12 |
| Precedence | list |
| Reply-To | d@davea.name |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3410.1323346575.27778.python-list@python.org> (permalink) |
| Lines | 72 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1323346575 news.xs4all.nl 6967 [2001:888:2000:d::a6]:56885 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:16825 |
Show key headers only | View raw
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. -- DaveA
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: sending a variable to an imported module Dave Angel <d@davea.name> - 2011-12-08 07:15 -0500
csiph-web