简单的双下划线作为变量名是什么意思?只是不要跟在其他字符后面

2024-04-25 08:24:14 发布

您现在位置:Python中文网/ 问答频道 /正文

当我在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]:

上面有一个______,这些变量作为内置变量的含义是什么?在

也没有得到_i_iii的意思,似乎这个东西只在IPython中定义。在

_oh显示一个dict,它存储ipython上的所有输出。在


Tags: 函数inforwindowstypediripythonbit
2条回答

_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)

根据IPythondocs_*值缓存最近输出的值:

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.

相反,_i*变量store recent inputs

_i, _ii, _iii: store previous, next previous and next-next previous inputs.

相关问题 更多 >