Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed1.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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'importing': 0.05; 'assignment': 0.07; 'kumar': 0.09; 'linear': 0.09; 'mess': 0.09; 'pointers': 0.09; 'subject:modules': 0.09; 'things,': 0.09; 'works.': 0.09; 'python': 0.11; 'def': 0.12; 'bindings.': 0.16; 'disconnect': 0.16; 'discussion.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'globals': 0.16; 'invocation': 0.16; 'namespace.': 0.16; 'sequence:': 0.16; 'subject:variable': 0.16; 'variables,': 0.16; 'wrote:': 0.18; 'library': 0.18; 'variable': 0.18; 'wed,': 0.18; 'module': 0.19; 'command': 0.22; 'import': 0.22; 'print': 0.22; '(by': 0.24; 'circular': 0.24; 'simpler': 0.24; 'equivalent': 0.26; 'header:In-Reply-To:1': 0.27; 'point': 0.28; '[1]': 0.29; "doesn't": 0.30; 'sets': 0.30; 'strongly': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'getting': 0.31; 'invoke': 0.31; 'loads': 0.31; 'sep': 0.31; 'file': 0.32; 'class': 0.32; 'another': 0.32; 'one,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'library.': 0.36; 'doing': 0.36; 'application': 0.37; 'two': 0.37; 'clear': 0.37; 'expected': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'previous': 0.38; 'to:addr:python.org': 0.39; 'enough': 0.39; 'how': 0.40; 'new': 0.61; 'simply': 0.61; 'simple': 0.61; 'name': 0.63; 'kind': 0.63; 'different': 0.65; 'due': 0.66; 'close': 0.67; 'yes': 0.68; 'line,': 0.68; 'qualified': 0.72; 'actually,': 0.84; 'affected.': 0.84; "everything's": 0.84; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=Sp/zPBxlI8IUhi+YMHPbGzSumEPH4ng5VGNs0jGi5as=; b=HpRWm9gH3o9ibA1aSD4Gn2tKcF9lCOk494piJCUB25kVGACaKPwf2froqxugDpoZWt krghENBSj+nGyeItpVubMNW96AdPCPXX/Zy7GBeRWDs97LYoXipzZSL8nPbOXUMeCUzD mhgcSI88WBEmMb9urNLxhCIAASpTEYrgeHcbRiGOP+rX7LmEmMflJ0aoqLh4nPPcyluw ZQo0c4F882dxpbhuG14c/XGyWDNQmbvtmGSdo1eCR030/98wGKVWjjxfjC17wp/ep1a5 TnuH8QncLcMxeAV0AjUqFz1mn5eWBAZPbwqF0g9Fcsreq+2PLgNnQFu+1ztO9mYw0A0d s3wA== MIME-Version: 1.0 X-Received: by 10.221.27.73 with SMTP id rp9mr480069vcb.29.1378905323221; Wed, 11 Sep 2013 06:15:23 -0700 (PDT) In-Reply-To: <1378903830.83908.YahooMailBasic@web190503.mail.sg3.yahoo.com> References: <1378903830.83908.YahooMailBasic@web190503.mail.sg3.yahoo.com> Date: Wed, 11 Sep 2013 23:15:23 +1000 Subject: Re: global variable across modules From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 51 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1378905331 news.xs4all.nl 15945 [2001:888:2000:d::a6]:56936 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:53964 On Wed, Sep 11, 2013 at 10:50 PM, chandan kumar wrote: > In Test2.py file I wanted to print the global value ,Debug_Value as 10.I'm not getting expected result.Please can any one point where exactly i'm doing wrong. > > Similarly , how can i use global variable inside a class and use the same value of global variable in different class?Is that possible?if Yes please give me some pointers on implementing. Python simply doesn't have that kind of global. What you have is module-level "variables" [1] which you can then import. But importing is just another form of assignment: # Test1.py Debug_Value = " " # Test2.py from Test1 import * # is exactly equivalent to Debug_Value = " " It simply sets that in Test2's namespace. There's no linkage across. (By the way, I strongly recommend NOT having the circular import that you have here. It'll make a mess of you sooner or later; you actually, due to the way Python loads things, have two copies of one of your modules in memory.) When you then reassign to Debug_Value inside Test1, you disconnect it from its previous value and connect it to a new one, and the assignment in the other module isn't affected. Here's a much simpler approach: # library.py foo = 0 def bar(): global foo foo += 1 # application.py import library library.bar() print(library.foo) This has a simple linear invocation sequence: you invoke the application from the command line, and it calls on its library. No confusion, no mess; and you can reference the library's globals by qualified name. Everything's clear and everything works. ChrisA [1] They're not really variables, they're name bindings. But close enough for this discussion.