[Tutor] global list

Albert-Jan Roskam fomcl at yahoo.com
Thu Apr 24 22:30:18 CEST 2014



________________________________
> From: Peter Otten <__peter__ at web.de>
>To: tutor at python.org 
>Sent: Thursday, April 24, 2014 1:49 PM
>Subject: Re: [Tutor] global list
> 
>
>Albert-Jan Roskam wrote:

>> ----- Original Message -----
>>> From: Steven D'Aprano <steve at pearwood.info>
>>> To: tutor at python.org
>>> Cc:
>>> Sent: Thursday, April 24, 2014 3:00 AM
>>> Subject: Re: [Tutor] global list
>>> 
>> 
>> <snip>
>> 
>>> You only need to define variables as global if you assign to them:
>>> 
>>> def function(x):
>>> global a
>>> a = [1, 2, 3, x]  # assignment to variable "a"
>> 
>> ah, thanks, I always wondered about that. But doesn't it make the function
>> (slightly) faster if you use 'global' when you only refer to that global
>> variable? You tell the interpreter that it is not needed to search for
>> that variable locally, so no time wasted on that. The code below indicates
>> that it makes NO difference (well, a whopping 2ns), but maybe for larger
>> functions it does?
>> 
>> albertjan at debian:~$ ipython
>> Python 2.7.3 (default, Mar 13 2014, 11:03:55)
>> 
>> In [1]: a = True
>> 
>> In [2]: def function():
>> ...:     x = True if a else False
>> ...:
>> 
>> In [3]: %timeit function()
>> 10000000 loops, best of 3: 122 ns per loop
>> 
>> In [4]: def function():
>> ...:     global a
>> ...:     x = True if a else False
>> ...:
>> 
>> In [5]: %timeit function()
>> 
>> 10000000 loops, best of 3: 120 ns per loop
>
>For functions whether a is global or not is determined at compile-time. Have 
>a look at the byte code for your functions:
>
>>>> def f():
>...     x = True if a else False
>... 
>>>> def g():
>...     global a
>...     x = True if a else False
>... 
>>>> dis.dis(f)
>  2           0 LOAD_GLOBAL              0 (a) 
>              3 POP_JUMP_IF_FALSE       12 
>              6 LOAD_CONST               1 (True) 
>              9 JUMP_FORWARD             3 (to 15) 
>        >>   12 LOAD_CONST               2 (False) 
>        >>   15 STORE_FAST               0 (x) 
>             18 LOAD_CONST               0 (None) 
>             21 RETURN_VALUE        
>>>> dis.dis(g)
>  3           0 LOAD_GLOBAL              0 (a) 
>              3 POP_JUMP_IF_FALSE       12 
>              6 LOAD_CONST               1 (True) 
>              9 JUMP_FORWARD             3 (to 15) 
>        >>   12 LOAD_CONST               2 (False) 
>        >>   15 STORE_FAST               0 (x) 
>             18 LOAD_CONST               0 (None) 
>             21 RETURN_VALUE        
>
>It is identical. Both functions "know" that a is a global name.
>A name can refer to a global or a local name, not both. One consequence is 
>this error:

Thanks! 'dis' is very useful. I don't use it often enough.

Another reason why I am sometimes inclined to use 'global': to explicitly mention that I am doing the one thing that all textbooks warn about, using an Evil Global Variable. But that's probably silly. As a side not, I find that variables (attributes) defined in __init__ are also much like globals, but that's probably a different discussion.



More information about the Tutor mailing list