[Tutor] Is implicit underscore assignment documented?

Terry Carroll carroll at tjc.com
Fri Jul 7 19:40:58 CEST 2006


Is implicit underscore assignment documented anywhere?  I've seen 
reference to this, and it seems to be simply that, when Python 
interactively prints a value in response to a user entering an expression, 
that value is assigned to the variable _.  

As far as I can tell, the following transcript shows its workings:

ActivePython 2.4.3 Build 12 (ActiveState Software Inc.) based on 
Python 2.4.3 (#69, Apr 11 2006, 15:32:42) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> _
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: name '_' is not defined
>>> a = 0
>>> _
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: name '_' is not defined
>>> a
0
>>> _
0
>>>
>>> a = 5
>>> _
0
>>> a
5
>>> _
5
>>> a = 5
>>> _
0
>>> a
5
>>> _
5
>>> #value unchanged, because a's value was not printed (the interpreter 
does not print None).
...
>>> a = 10
>>> print a
10
>>> _
5
>>> # value unchanged, because print is a statement, not an expression.
...
>>>

Is this documented anywhere?



More information about the Tutor mailing list