When I run ipython
on Windows 10 and execute dir
function, got this:
Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:57:15) [MSC v.1915 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 7.2.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: dir()
Out[1]:
['In',
'Out',
'_',
'__',
'___',
'__builtin__',
'__builtins__',
'__doc__',
'__loader__',
'__name__',
'__package__',
'__spec__',
'_dh',
'_i',
'_i1',
'_ih',
'_ii',
'_iii',
'_oh',
'exit',
'get_ipython',
'quit']
In [2]:
There is a _
, __
and a ___
above, what's the meaning of those variables as a built-in variable?
And also didn't get the meaning of _i
, _iii
, seems this thing only defined in IPython
.
_oh
shows a dict which stores all outputs on ipython.
According to the IPython docs, the _*
values cache the values of recent outputs:
The following variables always exist:
_
(a single underscore): stores previous output, like Python’s default interpreter.__
(two underscores): next previous.___
(three underscores): next-next previous.
Conversely, the _i*
variable store recent inputs:
_i
,_ii
,_iii
: store previous, next previous and next-next previous inputs.
_ is used for: 1. interpreter, 2. after name, 3. before name f.e. for ignoring values:
# Ignore a value
for _ in range(5)
print "Test"
# Ignore a value when unpacking
a, b, _, _ = my_method(var1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With