Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #94439 > unrolled thread
| Started by | candide <c.candide@laposte.net> |
|---|---|
| First post | 2015-07-23 03:24 -0700 |
| Last post | 2015-07-23 15:58 +0200 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.lang.python
global and loop control variable candide <c.candide@laposte.net> - 2015-07-23 03:24 -0700
Re: global and loop control variable Lorenzo Sutton <lorenzofsutton@gmail.com> - 2015-07-23 13:20 +0200
Re: global and loop control variable Steven D'Aprano <steve@pearwood.info> - 2015-07-23 22:31 +1000
Re: global and loop control variable Lorenzo Sutton <lorenzofsutton@gmail.com> - 2015-07-23 15:58 +0200
| From | candide <c.candide@laposte.net> |
|---|---|
| Date | 2015-07-23 03:24 -0700 |
| Subject | global and loop control variable |
| Message-ID | <f019fb82-d223-4b4e-9ad0-b56418eb7710@googlegroups.com> |
About global declarations, Python Language Ref (PLR) explains:
[https://docs.python.org/3.4/reference/simple_stmts.html#the-global-statement]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Names listed in a global statement must not be used
in the same code block textually preceding that global statement.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
What I understand is that the following code is incorrect:
# ---------------------------------------
def f():
x=42
global x
f()
print(x)
# ---------------------------------------
And indeed, executing this piece of code "raises" a warning :
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test.py:3: SyntaxWarning: name 'x' is assigned to before global declaration
global x
42
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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?
[toc] | [next] | [standalone]
| From | Lorenzo Sutton <lorenzofsutton@gmail.com> |
|---|---|
| Date | 2015-07-23 13:20 +0200 |
| Message-ID | <mailman.910.1437650834.3674.python-list@python.org> |
| In reply to | #94439 |
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."
# ---------------------------------------
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve@pearwood.info> |
|---|---|
| Date | 2015-07-23 22:31 +1000 |
| Message-ID | <55b0deb9$0$1664$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #94442 |
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. -- Steven
[toc] | [prev] | [next] | [standalone]
| From | Lorenzo Sutton <lorenzofsutton@gmail.com> |
|---|---|
| Date | 2015-07-23 15:58 +0200 |
| Message-ID | <mailman.913.1437659923.3674.python-list@python.org> |
| In reply to | #94446 |
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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web