开发者

python interpreter with ellipsis _ function?

开发者 https://www.devze.com 2023-01-12 06:39 出处:网络
>>> type(_) <type \'ellipsis\'> >>> 1 + 1 2 >>> _ 2 >开发者_JS百科>>
>>> type(_)
<type 'ellipsis'>

>>> 1 + 1
2
>>> _
2
>开发者_JS百科>> 

what's the usefulness of this _ function?


It just makes it easier to track intermediate values or to operate on the previously returned value.

>>> [x*x for x in range(5)]
[0, 1, 4, 9, 16]
>>> sum(_) # instead of having to type sum([0,1,4,9,16]) by hand
30


in case you use ipython it's part of ipythons [output caching system] - it just stores the previous output.

edit: oh, it seems to be implemented for the default python interpreter as well.

0

精彩评论

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