Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!border1.nntp.ams2.giganews.com!border3.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!feeder2.cambriumusenet.nl!feed.tweaknews.nl!194.109.133.83.MISMATCH!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!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; '"""': 0.05; 'pat': 0.05; 'dependency': 0.07; 'encouraging': 0.07; 'python': 0.09; 'facts': 0.09; 'subject:modules': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr:python-list': 0.10; 'suggest': 0.11; 'language': 0.14; 'constants': 0.16; 'hierarchy': 0.16; 'namespace,': 0.16; 'namespace.': 0.16; 'subject:class': 0.16; 'subject:instance': 0.16; 'variables),': 0.16; 'wrote:': 0.17; 'pointed': 0.17; 'variables': 0.17; 'code,': 0.18; 'module': 0.19; 'variable': 0.20; 'import': 0.21; 'constant': 0.22; 'wednesday,': 0.22; 'cc:2**0': 0.23; 'this:': 0.23; 'cc:no real name:2**0': 0.24; 'testing': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply- To:1': 0.25; 'header:User-Agent:1': 0.26; 'module.': 0.27; 'strongly': 0.27; 'lines': 0.28; 'statements': 0.29; 'classes': 0.30; 'keyword': 0.30; 'code': 0.31; 'could': 0.32; 'anyone': 0.33; 'know.': 0.33; 'another': 0.33; 'received:google.com': 0.34; 'needed': 0.35; 'exist': 0.35; 'especially': 0.35; 'subject:?': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'created': 0.36; 'michael': 0.36; 'but': 0.36; 'anything': 0.36; 'does': 0.37; 'level': 0.37; 'received:209': 0.37; 'received:209.85.216': 0.37; 'subject:: ': 0.38; 'comment': 0.38; 'fact': 0.38; 'object': 0.38; 'advice': 0.39; 'little': 0.39; 'skip:" 10': 0.40; 'back': 0.62; 'else.': 0.65; 'subject': 0.66; 'middle': 0.66; 'skip:$ 10': 0.66; 'manner': 0.74; 'yourself': 0.77; '2013': 0.84; 'furman': 0.84; 'needed:': 0.84; 'ethan': 0.91; 'hate': 0.93 X-Received: by 10.49.4.231 with SMTP id n7mr353019qen.34.1360289649715; Thu, 07 Feb 2013 18:14:09 -0800 (PST) Newsgroups: comp.lang.python Date: Thu, 7 Feb 2013 18:14:09 -0800 (PST) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=70.196.110.35; posting-account=h3aEwQoAAACiuqX-oR3gvCVFm8lLHoWj References: <15fe7153-a77a-49eb-9cf8-d4e59b4a2079@z9g2000vbx.googlegroups.com> <5db4add7-ab9c-4311-b3b6-24156cd6f248@z4g2000vbz.googlegroups.com> <5112FC02.1030805@stoneleaf.us> User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 70.196.110.35 MIME-Version: 1.0 Subject: Re: best way to share an instance of a class among modules? From: Rick Johnson To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=ISO-8859-1 Cc: python-list@python.org 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: , Message-ID: Lines: 34 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1360289657 news.xs4all.nl 6900 [2001:888:2000:d::a6]:56637 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:38396 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!). Now, whilst Micheal's advice is valid python code, i would strongly suggest against encouraging people to use the global keyword when they could simply declare the variable beforehand. I know what you are thinking: """ But python does not /require/ us to declare global variables if we use the global keyword""" Yes, i know. But i just hate to have variables created "magically" in the middle of execution! ESPECIALLY if these variables are going to be accessed from another module(s). *sniff-sniff* does anyone smell what i smell? Listen. I want to know every variable and every CONSTANT that will exist in the module namespace, and i want to know these facts BEFORE i read anything else. So please declare them at the top of each module. Hierarchy of module code structure: 0. shebang lines 1. stdlib import statements 2. 3rd party and dependency import statements 3. CONSTANTS 4. $globalVariables 5. module functions 6. module classes (bka: Object Definitions) 7. Testing Area So if you want to use "global variables" , (bka: Module level variables), then simply declare them with a None value like this: globalVariable = None But don't pat yourself on the back just yet! Place a little comment so the reader will understand that this variable is accessed from ANOTHER namespace. sharedCursor = None # Set by "Class.blah"; Whored out to X,Y and Z! In this manner i will not be blindsided by proper use of poor language features.