[pyar] Cuando el futuro afecta el presente
Alejandro Santos
listas en alejolp.com
Lun Oct 26 08:42:41 ART 2015
2015-10-24 3:23 GMT+02:00 Sebastian Bassi <sebastian.bassi en globant.com>:
> Traceback (most recent call last):
> File "/home/sbassi/foo.py", line 15, in <module>
> new_test_foo()
> File "/home/sbassi/foo.py", line 10, in new_test_foo
> print foo
> UnboundLocalError: local variable 'foo' referenced before assignment
>
Acceder/usar a variables globales en Python es una operacion costosas.
Como no tenes un 'global' de la variable, el parser asume que es una
variable local, que son mas rapidas de acceder que las globales. Pero
entonces tenes un foo local sin valor, de ahi sale el ULE.
>>> timeit.timeit("f()", "def f(): pass", number=10**8)
6.7549688816070557
>>> timeit.timeit("A=10", "A=None", number=10**8)
1.6623871326446533
>>> timeit.timeit("f()", "def f(): a=10", number=10**8)
7.6596798896789551
>>> 7.65 - 6.75
0.90000000000000036
>>> timeit.timeit("f()", "def f(): a=0;a=a", number=10**8)
9.4953410625457764
>>> timeit.timeit("A=0;A=A", "A=0", number=10**8)
3.4968819618225098
>>> timeit.timeit("A=0;A=A", "pass", number=10**8)
3.5225279331207275
>>> 9.49 - 6.75
2.7400000000000002
--
Alejandro Santos
More information about the pyar
mailing list