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


Groups > comp.lang.python > #16822

sending a variable to an imported module

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <bsemene@cyanide-studio.com>
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; 'parameter': 0.05; '===': 0.09; '[],': 0.09; 'doc,': 0.09; 'nameerror:': 0.09; 'output': 0.10; 'def': 0.13; '-1)': 0.16; 'debates': 0.16; 'object).': 0.16; 'obvious.': 0.16; 'singleton': 0.16; 'subject:sending': 0.16; 'subject:variable': 0.16; 'thanks,': 0.18; 'yet.': 0.18; 'trying': 0.21; 'maybe': 0.21; 'defined': 0.24; 'module': 0.26; "i'm": 0.26; 'variable': 0.28; '(this': 0.28; 'pass': 0.29; 'print': 0.29; 'imported': 0.30; 'threads': 0.30; "i've": 0.31; "can't": 0.32; 'header:User-Agent:1': 0.33; 'to:addr:python-list': 0.34; 'something': 0.35; 'test': 0.35; 'thread': 0.37; 'but': 0.37; 'list,': 0.37; 'think': 0.37; 'skip:_ 10': 0.37; 'enough': 0.38; "i'd": 0.39; 'why': 0.39; 'missing': 0.40; 'to:addr:python.org': 0.40; 'achieve': 0.61; 'internet': 0.64; 'relevant': 0.70; 'anytime': 0.74; "'test'": 0.84
Date Thu, 08 Dec 2011 12:28:21 +0100
From Bastien Semene <bsemene@cyanide-studio.com>
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:8.0) Gecko/20111105 Thunderbird/8.0
MIME-Version 1.0
To python-list@python.org
Subject sending a variable to an imported module
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
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.3407.1323344097.27778.python-list@python.org> (permalink)
Lines 37
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1323344097 news.xs4all.nl 6974 [2001:888:2000:d::a6]:41362
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:16822

Show key headers only | 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