Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!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.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'else:': 0.03; 'one?': 0.05; '"in': 0.09; 'executes': 0.09; 'restriction': 0.09; 'target,': 0.09; 'def': 0.13; 'declaration': 0.16; 'restriction,': 0.16; 'subject:variable': 0.16; 'wrote:': 0.16; 'later': 0.16; 'variable': 0.18; 'referring': 0.22; 'defined': 0.23; 'header:In- Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'parameters': 0.27; 'question': 0.27; 'to?': 0.27; 'print': 0.30; 'code': 0.30; 'another': 0.32; 'skip:- 30': 0.32; 'statement': 0.32; 'url:python': 0.33; 'message-id:@gmail.com': 0.34; 'received:google.com': 0.35; 'too': 0.36; 'url:org': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'names': 0.38; 'to:addr:python.org': 0.40; 'url:3': 0.60; 'charset:windows-1252': 0.62; '500': 0.63; 'url:4': 0.70; 'temperature': 0.84; 'received:172.16.1': 0.91; 'url:reference': 0.91 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=APk60B/Q+KQqS6wtKnztZ1NMy7lar6lE5iW3/kUPt1s=; b=DiZlGTUmSoX0aEPkDltmqfEzbqrH9j0tduLY6S/+jnP9KfLHC5zOJNML+xrKELZ4nn MpVYwIBKA/PigEXpy1i3hQUAs8fxk0CvlXSUn9L9m2mQHrjgrfXuBbZ11+/0z6DCWCMz qj9qv8I4IU+wF81TqwTQABNs1dv9aN+227xJNF4sF0OS1pt11ZHuSUISmbk3YmIeiivZ eCs3sm+VlFdJv/sPJIGbCZMF5E/OJn2UVLQFlmpa01RhtDxM93jnpNELYbEDJwRKLca5 fFxTPmr19jA+Z/Crh/FoigzuahLXv80MqJbG/EN4Z0VMXtLdCJcP2srwVW1FiDu75gIh zVeg== X-Received: by 10.194.238.193 with SMTP id vm1mr14676834wjc.57.1437650433388; Thu, 23 Jul 2015 04:20:33 -0700 (PDT) Subject: Re: global and loop control variable To: python-list@python.org References: From: Lorenzo Sutton Date: Thu, 23 Jul 2015 13:20:47 +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: Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Mailman-Approved-At: Thu, 23 Jul 2015 13:27:12 +0200 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: 56 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1437650834 news.xs4all.nl 2910 [2001:888:2000:d::a6]:42852 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:94442 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) > > f() > print(i) > # --------------------------------------- > > But, the later code executes silently without any warning: > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > 10 > 20 > 2 > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > > So my question is: what is the restriction about global as loop control variable the docs is referring to? > I think for situations like this one? # --------------------------------------- def f(): global temperature for temperature in range(1,3): print "In f temperature is:", temperature temperature = 500 print "temperature is now", temperature f() print"temperature is now:", temperature # temperature is now "broken" if temperature <= 100: print "Launching rocket" else: # this never happens print "temperature too high! Aborting launch." # ---------------------------------------