Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'modified': 0.07; 'tries': 0.07; 'variables': 0.07; 'attributes': 0.09; 'caching,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'variable,': 0.09; 'fine.': 0.16; 'hidden,': 0.16; "module's": 0.16; 'reasonable.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'scope.': 0.16; 'sense,': 0.16; 'subject:non': 0.16; 'variable.': 0.16; 'wrote:': 0.18; 'obviously': 0.18; 'seems': 0.21; 'import': 0.22; 'header:User- Agent:1': 0.23; 'case.': 0.24; 'earlier': 0.24; '(or': 0.24; 'sort': 0.25; 'possibly': 0.26; 'second': 0.26; 'header:X -Complaints-To:1': 0.27; 'point': 0.28; 'function': 0.29; 'thus': 0.29; 'program,': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'class': 0.32; 'problem': 0.35; 'anywhere': 0.35; 'classes': 0.35; 'possible.': 0.35; 'but': 0.35; 'there': 0.35; 'subject:skip:d 10': 0.36; 'charset:us-ascii': 0.36; 'subject:?': 0.36; 'changing': 0.37; 'too': 0.37; 'easily': 0.37; 'being': 0.38; 'sometimes': 0.38; 'whatever': 0.38; 'to:addr:python-list': 0.38; 'rather': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'called': 0.40; 'took': 0.61; 'first': 0.61; 'show': 0.63; 'name': 0.63; 'such': 0.63; 'places': 0.64; 'here': 0.66; 'between': 0.67; 'nobody': 0.68; 'pardon': 0.84; 'avoided.': 0.91; 'hand,': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dave Angel Subject: Re: Importing variables non-deterministic? Date: Mon, 19 Aug 2013 07:45:00 +0000 (UTC) References: <520f9054$0$30000$c3e8da3$5496439d@news.astraweb.com> <5211C5BD.5040209@rece.vub.ac.be> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: 174.32.174.30 User-Agent: XPN/1.2.6 (Street Spirit ; Linux) 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: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1376898318 news.xs4all.nl 15893 [2001:888:2000:d::a6]:33783 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:52676 Antoon Pardon wrote: > Op 17-08-13 17:01, Steven D'Aprano schreef: >> >> And here you re-import the name "y" from struct_global. That rebinds the >> current module's "y" with whatever value struct_global.y has *now*, >> rather than a second (or a minute, or an hour) earlier when the first >> import took place. Obviously at some point between the first import and >> the second import, struct_global.y must have been reassigned from -1 to >> 62. >> >> This goes to show why global variables are considered harmful, and why >> clean, modern program design tries to reduce the use of them as much as >> possible. Global variables are too easily modified by, well, *anything*. >> The sort of behaviour you are seeing is sometimes called "action at a >> distance" -- something, anything, anywhere in your program, possibly >> buried deep, deep down inside some function you might never suspect, is >> changing the global variable. > > I think you are overstating your case. Classes and functions are > variables too and in general nobody seems to have a problem with them > being global. > It's global *variables* that are to be avoided. constants like clsases and functions are fine. On the other hand, class attributes can be variable, and thus are to be avoided when reasonable. There *are* places where global variables make sense, such as for caching, or counting. But those are typically hidden, so they are global in lifetime, but not in scope. -- DaveA