Assigning a value to a single underscore _ in the Python / IPython interpreter

I created this function in Python 2.7 using ipython :

 def _(v): return v 

later, if I call _(somevalue) , I get _ = somevalue .

 in[3]: _(3) out[3]: 3 in[4]: print _ out[4]: 3 

Function has disappeared! If I call _(4) , I get:

 TypeError: 'int' object is not callable` 

Why? What is wrong with this feature?

+65
function python read-eval-print-loop interpreter ipython
Jul 10 '13 at 20:28
source share
3 answers

The Python interpreter assigns the last value of the _ expression.

This behavior is limited only by the REPL interpreter and is intended to aid in interactive coding sessions:

 >>> import math >>> math.pow(3.0, 5) 243.0 >>> result = _ >>> result 243.0 

The standard Python interpreter is long so as not to trample on user-defined values; if you yourself assign something else _ , then the interpreter will not overwrite it (technically speaking, the variable _ is an attribute of __builtin__ , your own assignments are "regular" globals). However, you are not using the standard Python interpreter; you are using IPython and this interpreter is not so careful.

IPython explicitly describes this behavior :

The following GLOBAL variables always exist (so do not overwrite them!):

  • [_] (one underscore): saves the previous output, for example the default interpreter Pythons.

[...]

Outside the Python interpreter, _ is used by default as the name of the translated text function (see gettext module ; external tools look for this function to extract the translatable strings).

In loops, using _ as the destination, readers read your code that you will ignore this value; for example [random.random() for _ in range(5)] to generate a list of 5 random float values.

+101
Jul 10 '13 at 20:30
source share

_ is a special variable in the interpreter; it is always assigned to the result of the previous expression. That way you won’t use it that way.

By the way, the problem seems to be related to the IPython shell, because your code works fine in a regular python shell:

In a regular python shell, when you assign something to the variable _ , then it will remain assigned only to this object and lose its special character.




Python shell:

 >>> 2*2 4 >>> _ #works as expected 4 >>> _ = 2 #after assignment, it magic functionality is gone >>> _*5 10 >>> _ 2 



IPython shell :

In IPython, _ behaves differently than the python _ shell; even if you assign it to some variable, then it will also be updated as soon as you do some calculations.

 In [1]: 2*2 Out[1]: 4 In [2]: _ Out[2]: 4 In [3]: _ = 10 In [4]: _*10 Out[4]: 100 In [5]: _ Out[5]: 100 



From IPython docs :

The following GLOBAL variables always exist (so do not overwrite them):

_: (one underscore): saves the previous output, e.g. Pythons default interpreter ...

From python docs :

The special identifier _ used in the interactive interpreter to save the result of the last evaluation; It is stored in __builtin__ . If not in interactive mode, _ has no special meaning and is not defined.

Note The name _ often used in conjunction with internationalization; refer to the documentation for the gettext module for more information on this convention.

+28
Jul 10 '13 at 20:30
source share

If you create a variable assigned to "_", it will mask / mask the system variable _.

-2
Jul 10 '13 at 20:38
source share



All Articles