Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 简单的双下划线作为变量名?;只是不要跟着其他的角色_Python_Ipython_Naming Conventions_Built In - Fatal编程技术网

Python 简单的双下划线作为变量名?;只是不要跟着其他的角色

Python 简单的双下划线作为变量名?;只是不要跟着其他的角色,python,ipython,naming-conventions,built-in,Python,Ipython,Naming Conventions,Built In,当我在Windows 10上运行ipython并执行dir函数时,得到以下结果: 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 h

当我在Windows 10上运行
ipython
并执行
dir
函数时,得到以下结果:

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]:
上面有一个
\uu
\u
和一个
\u
,这些变量作为内置变量的含义是什么

而且也没有得到
\u i
\u iii
的含义,似乎这件事只在
IPython
中定义过


\u oh
显示一个dict,它将所有输出存储在ipython上。

\u用于:1。翻译,2。名字后面是3。名前 f、 e.对于忽略值:

# Ignore a value 
for _ in range(5) 
    print "Test"

# Ignore a value when unpacking 
a, b, _, _ = my_method(var1)
根据IPython,值缓存最近输出的值:

以下变量始终存在:

  • (一个下划线):存储以前的输出,就像Python的默认解释器一样
  • (两个下划线):下一个上一个
  • (三个下划线):下一个上一个
相反,
\u i*
变量:

\u i
\u ii
\u iii
:存储上一个、下一个上一个和下一个上一个输入


它们在Python中没有特殊意义,ipython shell将它们用于history@juanpa.arrivillaga是的,我刚刚发现它们仅在
IPython
中定义。但是为什么要做这件事呢?