----- Original Message -----
> From: Steven D'Aprano <st...@pearwood.info>
> To: tutor@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@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
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor