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!newsfeed5.news.xs4all.nl!xs4all!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; 'example:': 0.03; 'beginner': 0.04; 'subject:module': 0.04; 'parameter': 0.05; 'python': 0.08; '===': 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; '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; 'header:In-Reply-To:1': 0.22; '(just': 0.23; 'defined': 0.24; 'cc:2**0': 0.24; 'settings': 0.24; 'module': 0.26; "i'm": 0.26; 'import': 0.27; 'variable': 0.28; '(this': 0.28; 'message- id:@mail.gmail.com': 0.28; 'pass': 0.29; 'print': 0.29; 'cc:addr:python.org': 0.29; 'imported': 0.30; 'threads': 0.30; "i've": 0.31; "can't": 0.32; 'test.': 0.34; 'something': 0.35; 'test': 0.35; 'received:209.85.161': 0.36; 'thread': 0.37; 'but': 0.37; 'list,': 0.37; 'received:google.com': 0.37; 'think': 0.37; 'skip:_ 10': 0.37; 'enough': 0.38; 'received:209.85': 0.38; 'put': 0.38; 'e.g.': 0.39; "i'd": 0.39; 'why': 0.39; 'missing': 0.40; 'received:209': 0.40; '2011': 0.61; 'design': 0.61; 'achieve': 0.61; 'your': 0.61; 'internet': 0.64; 'relevant': 0.70; 'anytime': 0.74; "'test'": 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=OqVTSRlRBCBN/hg0mR6LYr/6N0Fp/CexeoFdbwnAkvI=; b=qtE9VM+Fwx8RBIrBlM6LROMgLnlhBgb4dmyzF3qMHl8PJMcrgoQu3HIACzavvoWl/F q3+sol30pgCYKdS/04/zNtZN/YtI393SeqYe9FaRNXyCnr/eEmg0ZN0AoL2MpApLZ59D oo/5EAj0jBnHQEN4WrCyPbkWad7WInq5EAlZo= MIME-Version: 1.0 In-Reply-To: <4EE09F55.2050504@cyanide-studio.com> References: <4EE09F55.2050504@cyanide-studio.com> Date: Thu, 8 Dec 2011 11:55:20 +0000 Subject: Re: sending a variable to an imported module From: Arnaud Delobelle To: Bastien Semene Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org 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: 58 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1323345323 news.xs4all.nl 6981 [2001:888:2000:d::a6]:54379 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:16824 On 8 December 2011 11:28, 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 : > > =3D=3D=3D module.py =3D=3D=3D > def testf(): > =C2=A0print test > > > =3D=3D=3D main.py =3D=3D=3D > global test > test =3D 1 > > imported_module =3D __import__(module, globals(), locals(), [], -1) > > importmodule.testf() > > =3D=3D=3D output =3D=3D=3D > 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. Put it in its own module and import that module in the places where it is needed. E.g. in your example: =3D=3D settings.py =3D=3D test =3D 1 =3D=3D module.py =3D=3D import settings def testf(): print settings.testf =3D=3D main.py =3D=3D import module module.testf() In every module that you need 'test', import settings and you will be able to access test. This is why singletons are often not needed in Python (just like most other design patterns). --=20 Arnaud