È un nome temporaneo utilizzato in una list comprehension di Python 2.6 e precedenti. Python 2.7 e Python 3.x hanno risolto questo problema: l'elenco creato non è più accessibile fino al completamento della creazione.
In breve, era un dettaglio di implementazione a cui nessuno avrebbe mai dovuto fare affidamento.
Qui potete vedere che Python 2.7 foglie locals()
immutato, mentre Python 2.6 crea un breve dal vivo temporanea:
Python 2.7.2 (default, Jan 5 2012, 16:24:09)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-51)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def foo():
t = [list(locals().keys()) for x in range(1) ]
print(locals())
print(t[0])
>>> foo()
{'x': 0, 't': [['x']]}
['x']
>>>
Python 2.6.7 (r267:88850, Jan 5 2012, 16:18:48)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-51)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def foo():
t = [list(locals().keys()) for x in range(1) ]
print(locals())
print(t[0])
>>> foo()
{'x': 0, 't': [['_[1]', 'x']]}
['_[1]', 'x']
>>>
Python 3.x introduce un nuovo breve temporanei per la lista comprensioni vissuto chiamati .0
. Non essere tentato di usarlo per niente. Anche l'elenco intero di comprensione viene eseguito in uno spazio dei nomi separato in modo le variabili di loop non sono accessibili al di fuori del ciclo sia:
Python 3.2 (r32:88445, Jan 5 2012, 16:29:57)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-51)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def foo():
t = [list(locals().keys()) for x in range(1) ]
print(locals())
print(t[0])
>>> foo()
{'t': [['.0', 'x']]}
['.0', 'x']
Non solo è un dettaglio di implementazione del male, è stato rimosso in Python 2.7. Non usarlo – Duncan