Path: csiph.com!optima2.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!bcyclone01.am1.xlned.com!bcyclone01.am1.xlned.com!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!194.109.133.91.MISMATCH!newsfeed.xs4all.nl!newsfeed8.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.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'one?': 0.05; 'correct.': 0.07; '"in': 0.09; 'highlight': 0.09; 'meaningful': 0.09; 'restriction': 0.09; 'target,': 0.09; ':-)': 0.12; 'syntax': 0.13; 'def': 0.13; 'thu,': 0.15; 'variables': 0.15; 'declaration': 0.16; 'example)': 0.16; 'restriction,': 0.16; 'subject:variable': 0.16; 'sutton': 0.16; 'variable.': 0.16; 'wrote:': 0.16; 'abuse': 0.18; 'variable': 0.18; '>>>': 0.20; '2015': 0.20; '(see': 0.20; 'enforce': 0.22; 'referring': 0.22; 'defined': 0.23; 'header:In- Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'example': 0.26; 'parameters': 0.27; 'question': 0.27; 'to?': 0.27; 'behaviour': 0.29; 'freedom,': 0.29; 'launch': 0.29; 'raise': 0.29; 'print': 0.30; 'code': 0.30; 'error.': 0.31; 'another': 0.32; 'especially': 0.32; 'statement': 0.32; 'url:python': 0.33; "d'aprano": 0.33; 'steven': 0.33; 'message-id:@gmail.com': 0.34; '(for': 0.34; 'received:google.com': 0.35; 'next': 0.35; 'could': 0.35; 'but': 0.36; 'should': 0.36; 'url:org': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'difference': 0.38; 'names': 0.38; 'does': 0.39; 'to:addr:python.org': 0.40; 'future': 0.60; 'url:3': 0.60; 'your': 0.60; 'entire': 0.61; 'programs': 0.62; 'charset:windows-1252': 0.62; 'between': 0.65; 'url:4': 0.70; 'jul': 0.72; 'course...': 0.84; 'program."': 0.84; 'temperature': 0.84; 'received:172.16.1': 0.91; 'url:reference': 0.91; 'rocket': 0.95 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-type:content-transfer-encoding; bh=xJPdlnKVtc7HF71leJHX7fxJa0e+O6fSJI7jUZB2CkA=; b=if/01miIuB6ATFXdWumr4n0QVGpOK8lOqGEAqjw+mfgBN47osOLjOgJuvFa8xWdDLz Q3OhWCsvXWj6xKUW3UOpyNqwwdAN1L3cylB+kYtWgnnZY+ayzYUuf3BYkyy2CEDvpZcP IlcRRoi9R34Cf8C2cvdlNyABczi4TsQoxHbF0jR//cPIl/drZh/zMgIfPwX6NwzGrYbs iVgNUOfBGv2hn915uH4yW6jCxdyks7jdJt5otuRuKOhsmrClOPa1NdjvZet4zzQ+w7Rv RJkEuhsFa0OnzZO0P1CXm9tEnPIk22NHhTP4zmGhnlgdn7tzqfWQixEFYyUkEm8+d6MX FiVw== X-Received: by 10.194.171.129 with SMTP id au1mr17252689wjc.115.1437659921918; Thu, 23 Jul 2015 06:58:41 -0700 (PDT) Subject: Re: global and loop control variable To: python-list@python.org References: <55b0deb9$0$1664$c3e8da3$5496439d@news.astraweb.com> From: Lorenzo Sutton Date: Thu, 23 Jul 2015 15:58:55 +0200 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.1.0 MIME-Version: 1.0 In-Reply-To: <55b0deb9$0$1664$c3e8da3$5496439d@news.astraweb.com> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ 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: 54 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1437659923 news.xs4all.nl 2838 [2001:888:2000:d::a6]:40452 X-Complaints-To: abuse@xs4all.nl X-Received-Bytes: 6012 X-Received-Body-CRC: 2500341804 Xref: csiph.com comp.lang.python:94448 On 23/07/2015 14:31, Steven D'Aprano wrote: > On Thu, 23 Jul 2015 09:20 pm, Lorenzo Sutton wrote: > >> On 23/07/2015 12:24, candide wrote: >>> Now, global declaration has another restriction, as PLR explains: >>> > [https://docs.python.org/3.4/reference/simple_stmts.html#the-global-statement] >>> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ >>> Names listed in a global statement must not be defined as formal >>> parameters or in a for loop control target, >>> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ >>> >>> What I understand is that the following is a must-not-code: >>> >>> def f(): >>> global i >>> for i in range(1,3): >>> print(10*i) > [...] >>> So my question is: what is the restriction about global as loop control >>> variable the docs is referring to? > > You are correct. The above example is exactly the restriction mentions. The > very next paragraph in the docs says: > > "CPython implementation detail: The current implementation does not enforce > the two restrictions, but programs should not abuse this freedom, as future > implementations may enforce them or silently change the meaning of the > program." > > In other words, the behaviour of global loop variables is not guaranteed, > and you should not use it even if the compiler/interpreter fails to raise a > syntax error. > > >> I think for situations like this one? >> >> def f(): >> global temperature >> for temperature in range(1,3): >> print "In f temperature is:", temperature > > > There's no meaningful difference between the example Candide gave (for i in > range) and the example you give (for temperature in range). They both use a > global for the loop variable. Only the names differ. Of course... it was just to highlight that it could be potentially, especially if your programme is going to launch a rocket - eventually (see my entire code example) :-) Lorenzo.