在Python / IPython解释器中为单个下划线_赋值

时间:2022-12-15 20:44:38

I created this function in Python 2.7 with ipython:

我使用ipython在Python 2.7中创建了这个函数:

def _(v):
    return v

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

后来如果我调用_(somevalue),我得到_ = somevalue。

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

The function has disappeared! If I call _(4) I get:

功能消失了!如果我打电话给_(4)我得到:

TypeError: 'int' object is not callable`

Why? What's wrong with this function?

为什么?这个功能有什么问题?

3 个解决方案

#1


101  

The Python interpreter assigns the last expression value to _.

Python解释器将最后一个表达式值赋给_。

This behaviour is limited to the REPL interpreter only, and is intended to assist in interactive coding sessions:

此行为仅限于REPL解释器,旨在帮助进行交互式编码会话:

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

The standard Python interpreter goes to some length to not trample on user-defined values though; if you yourself assign something else to _ then the interpreter will not overwrite that (technically speaking, the _ variable is a __builtin__ attribute, your own assignments are 'regular' globals). You are not using the standard Python interpreter though; you are using IPython, and that interpreter is not that careful.

标准的Python解释器有一定篇幅,但不会践踏用户定义的值;如果你自己为_分配其他东西,那么解释器就不会覆盖它(从技术上讲,_变量是__builtin__属性,你自己的赋值是'常规'全局变量)。你没有使用标准的Python解释器;你正在使用IPython,那个解释器并不那么谨慎。

IPython documents this behaviour explicitly:

IPython明确记录了这种行为:

The following GLOBAL variables always exist (so don’t overwrite them!):

以下GLOBAL变量始终存在(因此不要覆盖它们!):

  • [_] (a single underscore) : stores previous output, like Python’s default interpreter.
  • [_](单个下划线):存储以前的输出,如Python的默认解释器。

[...]

[...]

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

在Python解释器之外,_按惯例用作可翻译文本函数的名称(请参阅gettext模块;外部工具查找该函数以提取可翻译的字符串)。

In loops, using _ as an assignment target tells readers of your code that you are going to ignore that value; e.g. [random.random() for _ in range(5)] to generate a list of 5 random float values.

在循环中,使用_作为赋值目标告诉读者您的代码将忽略该值;例如[random_random()for _ in range(5)]生成5个随机浮点值的列表。

#2


28  

_ is a special variable in interpreter, it is always assigned to the result of previous expression. So, you shoudn't use it like that.

_是解释器中的特殊变量,它始终分配给前一个表达式的结果。所以,你不应该那样使用它。

BTW the problem seems to be related to IPython shell, because your code works fine in normal python shell:

BTW问题似乎与IPython shell有关,因为你的代码在普通的python shell中工作正常:

In normal python shell when you assign anything to the variable _ then it'll remain assigned to that object only, and looses it special behaviour.

在普通的python shell中,当你为变量_分配任何东西时,它仍然只被分配给那个对象,并且失去了它的特殊行为。


Python shell:

Python shell:

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

IPython shell:

IPython shell:

In IPython _ behaves differently than python shell's _; even if you assign it to some variable then also it is going to be updated as soon as you do some calculation.

在IPython中_的行为与python shell的_不同;即使你将它分配给某个变量,它也会在你做一些计算后立即更新。

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's docs:

来自IPython的文档:

The following GLOBAL variables always exist (so don’t overwrite them!):

以下GLOBAL变量始终存在(因此不要覆盖它们!):

_ : (a single underscore) : stores previous output, like Python’s default interpreter. ..

_ :(单个下划线):存储以前的输出,如Python的默认解释器。 ..

From python docs:

从python文档:

The special identifier _ is used in the interactive interpreter to store the result of the last evaluation; it is stored in the __builtin__ module. When not in interactive mode, _ has no special meaning and is not defined.

特殊标识符_用于交互式解释器中以存储上次评估的结果;它存储在__builtin__模块中。当不处于交互模式时,_没有特殊含义,也没有定义。

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

注意:名称_通常与国际化一起使用;有关此约定的更多信息,请参阅gettext模块的文档。

#3


-2  

If you create a variable assigned to "_" it gets masked/masks the system variable _.

如果创建一个分配给“_”的变量,它将被屏蔽/屏蔽系统变量_。

#1


101  

The Python interpreter assigns the last expression value to _.

Python解释器将最后一个表达式值赋给_。

This behaviour is limited to the REPL interpreter only, and is intended to assist in interactive coding sessions:

此行为仅限于REPL解释器,旨在帮助进行交互式编码会话:

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

The standard Python interpreter goes to some length to not trample on user-defined values though; if you yourself assign something else to _ then the interpreter will not overwrite that (technically speaking, the _ variable is a __builtin__ attribute, your own assignments are 'regular' globals). You are not using the standard Python interpreter though; you are using IPython, and that interpreter is not that careful.

标准的Python解释器有一定篇幅,但不会践踏用户定义的值;如果你自己为_分配其他东西,那么解释器就不会覆盖它(从技术上讲,_变量是__builtin__属性,你自己的赋值是'常规'全局变量)。你没有使用标准的Python解释器;你正在使用IPython,那个解释器并不那么谨慎。

IPython documents this behaviour explicitly:

IPython明确记录了这种行为:

The following GLOBAL variables always exist (so don’t overwrite them!):

以下GLOBAL变量始终存在(因此不要覆盖它们!):

  • [_] (a single underscore) : stores previous output, like Python’s default interpreter.
  • [_](单个下划线):存储以前的输出,如Python的默认解释器。

[...]

[...]

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

在Python解释器之外,_按惯例用作可翻译文本函数的名称(请参阅gettext模块;外部工具查找该函数以提取可翻译的字符串)。

In loops, using _ as an assignment target tells readers of your code that you are going to ignore that value; e.g. [random.random() for _ in range(5)] to generate a list of 5 random float values.

在循环中,使用_作为赋值目标告诉读者您的代码将忽略该值;例如[random_random()for _ in range(5)]生成5个随机浮点值的列表。

#2


28  

_ is a special variable in interpreter, it is always assigned to the result of previous expression. So, you shoudn't use it like that.

_是解释器中的特殊变量,它始终分配给前一个表达式的结果。所以,你不应该那样使用它。

BTW the problem seems to be related to IPython shell, because your code works fine in normal python shell:

BTW问题似乎与IPython shell有关,因为你的代码在普通的python shell中工作正常:

In normal python shell when you assign anything to the variable _ then it'll remain assigned to that object only, and looses it special behaviour.

在普通的python shell中,当你为变量_分配任何东西时,它仍然只被分配给那个对象,并且失去了它的特殊行为。


Python shell:

Python shell:

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

IPython shell:

IPython shell:

In IPython _ behaves differently than python shell's _; even if you assign it to some variable then also it is going to be updated as soon as you do some calculation.

在IPython中_的行为与python shell的_不同;即使你将它分配给某个变量,它也会在你做一些计算后立即更新。

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's docs:

来自IPython的文档:

The following GLOBAL variables always exist (so don’t overwrite them!):

以下GLOBAL变量始终存在(因此不要覆盖它们!):

_ : (a single underscore) : stores previous output, like Python’s default interpreter. ..

_ :(单个下划线):存储以前的输出,如Python的默认解释器。 ..

From python docs:

从python文档:

The special identifier _ is used in the interactive interpreter to store the result of the last evaluation; it is stored in the __builtin__ module. When not in interactive mode, _ has no special meaning and is not defined.

特殊标识符_用于交互式解释器中以存储上次评估的结果;它存储在__builtin__模块中。当不处于交互模式时,_没有特殊含义,也没有定义。

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

注意:名称_通常与国际化一起使用;有关此约定的更多信息,请参阅gettext模块的文档。

#3


-2  

If you create a variable assigned to "_" it gets masked/masks the system variable _.

如果创建一个分配给“_”的变量,它将被屏蔽/屏蔽系统变量_。