Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of simply __ double underscore as a variable name? Just __ not follow another chars

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.

like image 682
roachsinai Avatar asked Sep 07 '25 14:09

roachsinai


2 Answers

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.

like image 188
snakecharmerb Avatar answered Sep 09 '25 03:09

snakecharmerb


_ 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)
like image 21
გენო მუმლაძე Avatar answered Sep 09 '25 03:09

გენო მუმლაძე