Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '---------': 0.05; 'case.': 0.05; '"__main__":': 0.07; '__name__': 0.07; 'python': 0.09; '__future__': 0.09; 'overwrite': 0.09; 'subject:modules': 0.09; 'def': 0.10; 'language': 0.14; 'assignment?': 0.16; 'from:addr:torriem': 0.16; 'from:name:michael torrie': 0.16; 'hides': 0.16; "module's": 0.16; 'namespace.': 0.16; 'python3:': 0.16; 'subject:class': 0.16; 'subject:instance': 0.16; 'wrote:': 0.17; 'pointed': 0.17; 'module': 0.19; 'variable': 0.20; 'import': 0.21; 'of.': 0.22; 'wednesday,': 0.22; 'needed.': 0.23; 'statement': 0.23; 'least': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; '(which': 0.26; '--------': 0.28; 'behavior.': 0.29; 'keyword': 0.30; 'function': 0.30; 'code': 0.31; 'johnson': 0.32; 'to:addr:python-list': 0.33; 'times.': 0.33; 'another': 0.33; 'agree': 0.34; 'needed': 0.35; 'pm,': 0.35; 'subject:?': 0.35; 'something': 0.35; 'received:org': 0.36; 'michael': 0.36; 'but': 0.36; 'message-id:@gmail.com': 0.36; 'should': 0.36; 'skip:p 20': 0.36; 'itself': 0.37; 'subject:: ': 0.38; 'fact': 0.38; 'object': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'header:Received:5': 0.40; 'matter': 0.61; 'between': 0.63; 'subject': 0.66; '2013': 0.84; 'furman': 0.84; 'needed:': 0.84; 'ethan': 0.91; 'rick': 0.91 X-Virus-Scanned: amavisd-new at torriefamily.org Date: Thu, 07 Feb 2013 23:05:59 -0700 From: Michael Torrie User-Agent: Mozilla/5.0 (X11; Linux i686; rv:10.0.12) Gecko/20130105 Thunderbird/10.0.12 MIME-Version: 1.0 To: python-list@python.org Subject: Re: best way to share an instance of a class among modules? References: <15fe7153-a77a-49eb-9cf8-d4e59b4a2079@z9g2000vbx.googlegroups.com> <5db4add7-ab9c-4311-b3b6-24156cd6f248@z4g2000vbz.googlegroups.com> <5112FC02.1030805@stoneleaf.us> <5b81481d-b7ee-40a2-92ba-4bfb174e1694@googlegroups.com> In-Reply-To: <5b81481d-b7ee-40a2-92ba-4bfb174e1694@googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit 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: 39 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1360303575 news.xs4all.nl 6866 [2001:888:2000:d::a6]:54125 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:38411 On 02/07/2013 07:14 PM, Rick Johnson wrote: > On Wednesday, February 6, 2013 7:36:28 PM UTC-6, Ethan Furman wrote: >> As Michael Torrie pointed out, the 'global' keyword is needed: > > Wrong. The global keyword is in fact NOT needed and something i > consider to be another wart of the language (PyWart on this subject > coming soon!). Actually in both Python 2 and 3, the global keyword is, in fact needed. Otherwise the name binding in the function hides the global name. So no matter if you declare the variable in the module itself (which yes I agree you should always do!), you need the global keyword in the function if you want to overwrite the name in the module's global namespace. Observe the following code which I tested on both python2 and python3: -------- from __future__ import print_function my_global_var = 4 def test_func(): #global my_global_var my_global_var = 6 if __name__ == "__main__": print(my_global_var) test_func() print(my_global_var) --------- Without the global statement, you get 4 printed out both times. At least on the python interpreters I have handy. With the global statement uncommented, I get 4 and 6. Do you understand the difference between binding a name to an object and variable assignment? Because that's the reason we see this behavior. This would be a gotcha in this case. Not a wart, but simply something to be aware of.