开发者

Unmasking last printed expression variable

I have just started studying Python.

I am referring to the tutoria开发者_如何转开发ls at http://docs.python.org/tutorial/

Quoting from the tutorial about the variable _

This variable should be treated as read-only by the user. Don’t explicitly assign a value to it — you would create an independent local variable with the same name masking the built-in variable with its magic behavior.

Suppose that I have masked the variable by assigning it to a number like _ = 10. Is it possible to unmask it again to refer it to the last printed expression? If so, how?

Thanks.


del _ unmasks the redefined _:

>>> 7*2
>>> _
14
>>> _ = 88
>>> _
88
>>> 1+2
3
>>> _
88
>>> del _
>>> _
88
>>> 1+2
3
>>> _
3


del _ will do it.

In [1]: _ = 1

In [2]: _
Out[2]: 1

In [3]: 2
Out[3]: 2

In [4]: _
Out[4]: 1

In [5]: del _

In [6]: 3
Out[6]: 3

In [7]: _
Out[7]: 3


You've got to destroy your local object, i.e. del _

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜