Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed4.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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'python,': 0.02; 'value,': 0.04; 'modified': 0.07; 'nasty': 0.07; 'tries': 0.07; 'variables': 0.07; 'attributes': 0.09; 'correct,': 0.09; 'discriminate': 0.09; 'explanation': 0.09; 'function,': 0.09; 'latter': 0.09; 'naturally': 0.09; 'variable,': 0.09; 'variables.': 0.09; 'python': 0.11; 'def': 0.12; 'bug': 0.12; 'wrote': 0.14; 'constants.': 0.16; 'fine.': 0.16; 'func': 0.16; 'lambda': 0.16; "module's": 0.16; 'reasonable.': 0.16; 'statement.': 0.16; 'subject:non': 0.16; 'sure.': 0.16; 'variable.': 0.16; 'wrote:': 0.18; 'obviously': 0.18; 'variable': 0.18; 'later': 0.20; 'meant': 0.20; 'seems': 0.21; '>>>': 0.22; 'code,': 0.22; 'import': 0.22; 'aug': 0.22; 'header:User-Agent:1': 0.23; 'case.': 0.24; 'instead.': 0.24; "shouldn't": 0.24; 'string,': 0.24; 'earlier': 0.24; 'mon,': 0.24; '(or': 0.24; 'sort': 0.25; 'possibly': 0.26; 'second': 0.26; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'function': 0.29; 'rest': 0.29; 'skip:p 30': 0.29; 'generally': 0.29; 'possibility': 0.29; 'thus': 0.29; 'change,': 0.30; 'originally': 0.30; 'strongly': 0.30; "i'm": 0.30; 'program,': 0.31; 'went': 0.31; 'code': 0.31; '>>>>': 0.31; 'constant': 0.31; "d'aprano": 0.31; 'int,': 0.31; 'piece': 0.31; 'steven': 0.31; 'class': 0.32; 'another': 0.32; 'says': 0.33; 'cases': 0.33; 'problem': 0.35; 'agree': 0.35; 'anywhere': 0.35; 'classes': 0.35; 'possible.': 0.35; 'objects': 0.35; 'point.': 0.35; 'but': 0.35; 'there': 0.35; '+0200,': 0.36; 'i.e.': 0.36; 'subject:skip:d 10': 0.36; 'subject:?': 0.36; 'should': 0.36; 'changing': 0.37; 'too': 0.37; 'two': 0.37; 'easily': 0.37; 'being': 0.38; 'sometimes': 0.38; 'whatever': 0.38; 'to:addr:python-list': 0.38; 'list,': 0.38; 'rather': 0.38; 'ability': 0.39; 'to:addr:python.org': 0.39; 'changed': 0.39; 'enough': 0.39; 'called': 0.40; 'read': 0.60; 'dave': 0.60; 'problems.': 0.60; 'most': 0.60; 'took': 0.61; 'course': 0.61; 'first': 0.61; 'making': 0.63; 'show': 0.63; 'information': 0.63; 'name': 0.63; 'such': 0.63; 'soon': 0.63; 'myself': 0.63; 'different': 0.65; 'here': 0.66; 'between': 0.67; 'nobody': 0.68; 'alone.': 0.84; 'disagreement': 0.84; 'horrible': 0.84; 'idiot,': 0.84; 'pardon': 0.84; 'received:195.238': 0.84; 'received:195.238.6': 0.84; 'received:belgacom.be': 0.84; 'received:isp.belgacom.be': 0.84; 'sweeping': 0.84; 'technically': 0.84; 'angel': 0.91; 'avoided.': 0.91; 'hand,': 0.93; '2013': 0.98 X-Belgacom-Dynamic: yes X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ApMBAJ5WElJbs80n/2dsb2JhbAANTcBIgneBOYMUBAEBAQMBOEARCxgJFg8JAwIBAgFFEwgChUWCKBmkGYoSiAeQY4QSA5dklHM Date: Mon, 19 Aug 2013 19:40:49 +0200 From: Antoon Pardon User-Agent: Mozilla/5.0 (X11; Linux i686; rv:17.0) Gecko/20130704 Icedove/17.0.7 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Importing variables non-deterministic? References: <520f9054$0$30000$c3e8da3$5496439d@news.astraweb.com> <5211C5BD.5040209@rece.vub.ac.be> <52124e81$0$29986$c3e8da3$5496439d@news.astraweb.com> In-Reply-To: <52124e81$0$29986$c3e8da3$5496439d@news.astraweb.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed 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: 91 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1376934119 news.xs4all.nl 15911 [2001:888:2000:d::a6]:50127 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:52702 Op 19-08-13 18:57, Steven D'Aprano schreef: > On Mon, 19 Aug 2013 10:16:36 +0200, Antoon Pardon wrote: > >> Op 19-08-13 09:45, Dave Angel schreef: >>> 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. >> >> Python has no constants. Classes and functions can be changed just like >> any other variable. I agree that classes and function are generally >> meant to be constant, but often enought so are global int variables. > > You are technically correct, but missing the point. If I wrote code that > went around reassigning names from one function to another function, I > would very likely soon work myself into a state of utter confusion: > > def func(x): > ... > > # later > save_func = func > func = lambda x, y: do_stuff(x, 3*y)-4 > result = something_that_calls_func() > func = save_func > > > Nasty, horrible code, yes? But it's nasty and horrible because "func" is > bound to a function, it would be equally nasty and horrible if it was a > data type (a string, a list, an int, a flag, ...) instead. > > Since classes and functions are First Class objects in Python, naturally > if you treat them as global *variables* rather than global *constants* > you can end up with problems. The problem is not the global part alone. > Global constants, or pseudo-constant-by-convention-only, are fine. I don't think there is a big disagreement on this, I just thought you overstated your case as you originally worded it. >> And some of those that do change, only do so in the initialisation phase >> and should be considered constant for the rest of the program. >> >> My point was that Steven has no way of knowing what exactly is going on >> here and so shouldn't be making such a sweeping statement. > > On the contrary, I can read the Original Poster's description of the > problem, and then use my ability to reason to deduce what the most likely > explanation was. > > Now of course I might be wrong. I don't claim infallibility or > omniscience. But I'm not an idiot, and if the OP says that a global > variable has one value at one time, and then some time later has a > different value, there are two likely possibilities: > > * The variable was changed by some other piece of code, i.e. it > actually was being used as a *global variable*. Sure. But was that because it was intended to be used as a variable or was the intention to use it as a constant but because of a bug it was changed anyway? I don't think you have enough information to discriminate between these two cases and for the possibility of this being the latter case I found your disgression into the harm of global variable too strongly worded. -- Antoon Pardon.