- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
发出 del 语句时:
del var
它不应该从已知变量列表中删除吗?python 解释器不应该吐出“未解析的引用”错误吗?
或者只是简单地删除对象并留下名称 (var) 不指向任何地方?为什么这种行为会有用?在什么情况下?
此外,我只是在谈论删除单个变量。不是 del list[3] 或类似的东西。
注意:我问的是这条 python 的行为是否意味着那样。在什么情况下,它仍然有用。
编辑:Charles Addis 给出了详细的解释。我也承认我错误地将 pycharm 行为误认为是官方 python 的行为。我现在正在尝试 ipython 以及官方 python 交互式 shell。尽管这是我的错误,但我很高兴我学到了很多关于 python 变量以及一些 python 调试命令的知识。
最佳答案
不确定你在问什么,因为很明显这就是发生的事情......
>>> x
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined
>>> x = 10
>>> 'x' in vars()
True
>>> vars()['x']
10
>>> del x
>>> 'x' in vars()
False
>>> x
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined
如您所见,Python 允许从 locals()
中的当前范围访问有效标识符(并且 vars()
的工作方式类似 - 还有 dir(obj)
将显示对象的属性)...所以 del 本质上是将其从内存中删除,这也将其从这些函数返回的数据结构中删除。所以标识符不再有效。它不像在 C 中,您可以释放一些内存并将值设置为 NULL
。
>>> x = 10
>>> def func():
... global x
... del x
...
>>> x
10
>>> func()
>>> x
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined
>>>
Raymond 提到对象本身(阅读:标识符指向的内存中的实际数据,由于缺乏更详细的解释)仅在其引用计数变为 0 时才被释放,他提出了一个很好的观点。IMO 他可以在 python 解释器中详细说明这一点,所以我会尝试一下。
我们将使用 ID 函数来证明这一点:
Help on built-in function id in module __builtin__:
id(...)
id(object) -> integer
Return the identity of an object. This is guaranteed to be unique among
simultaneously existing objects. (Hint: it's the object's memory address.)
注意这里发生的事情,你会看到对于不可变类型,不同的变量引用相同的内存(字符串在 python 中是不可变的并且是 interned 的,这意味着每个唯一字符串只存储一个副本 - 在 ruby ,符号是内部字符串)。
>>> import sys
>>> x = 10 # 10 is a common value, probably exists in memory already
>>> sys.getrefcount(x)
26
>>> id(x) # memory location of x
140266396760096
>>> y = x
>>> id(y) == id(x)
True
>>> z = 10
>>> id(z) == id(y) == id(x)
True
>>> sys.getrefcount(y)
28
>>> sys.getrefcount(z)
28
>>> del y, z
>>> sys.getrefcount(x)
26
>>> del x
>>> x = 'charlie'
>>> id(x)
4442795056
>>> y = 'charlie'
>>> z = x
>>> id(x) == id(y) == id(z)
True
>>> sys.getrefcount(x)
4
>>> sys.getrefcount(y)
4
>>> sys.getrefcount(z)
4
>>> del y
>>> del x
>>> sys.getrefcount(z) # will be two because this line is an additional reference
2
>>> id(z) # pay attention to this memory location because this
4442795056 # is the last remaining reference to 'charlie', and
>>> del z # when it goes out of scope 'charlie' is removed from
>>> # memory.
>>> id('charlie') # This has a different memory location because 'charlie'
4442795104 # had to be re-created.
首先我们设置标识符 'x' == 10,一个普通的整数值。由于 10 是一个如此常见的值,几乎可以保证进程内存中的某些内容已经具有该值。由于整数在 Python 中是不可变的,我们只需要存储在内存中的每个唯一值的一个副本。在这种情况下,内存中还有 24 个对 10 的其他引用。设置 x = 10
创建第 25 个引用,调用 sys.getrefcount(x)
是第 26 个引用(尽管它很快就会超出范围)。当我们设置 y = 10
和 z = x
时,我们知道它们都指向相同的数据,因为它们都具有相同的内存位置。调用 del
会改变引用计数,但即使删除了所有 3 个整数,整数 10 仍然存在于内存中。
接下来我们创建集合 x = 'charlie'
,然后是 y = 'charlie'
,最后是 z = x
。您可以看到所有这些变量都具有相同的内存地址。一旦我们删除了所有这些变量,就不会再有对 'charlie'
的引用。我们可以通过调用 id('charlie')
来验证这一点,这将产生一个不同的内存地址,这意味着当我们调用该函数时该字符串不存在于内存中。
还需要注意的是 'charlie'
和 10
在内存中的位置。 10
的内存地址明显高于 Charlie。这是因为它们存在于不同的内存位置。 'charlie'
存在于堆上,而 10
存在于堆栈上。
>>> hex(id(10)) # high address, this is on the stack
'0x7f9250c0b820'
>>> hex(id('charlie')) # lower address, this is on the heap
'0x108cfac60
关于python (deI) 语句和 python 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40099427/
我正在处理一组标记为 160 个组的 173k 点。我想通过合并最接近的(到 9 或 10 个组)来减少组/集群的数量。我搜索过 sklearn 或类似的库,但没有成功。 我猜它只是通过 knn 聚类
我有一个扁平数字列表,这些数字逻辑上以 3 为一组,其中每个三元组是 (number, __ignored, flag[0 or 1]),例如: [7,56,1, 8,0,0, 2,0,0, 6,1,
我正在使用 pipenv 来管理我的包。我想编写一个 python 脚本来调用另一个使用不同虚拟环境(VE)的 python 脚本。 如何运行使用 VE1 的 python 脚本 1 并调用另一个 p
假设我有一个文件 script.py 位于 path = "foo/bar/script.py"。我正在寻找一种在 Python 中通过函数 execute_script() 从我的主要 Python
这听起来像是谜语或笑话,但实际上我还没有找到这个问题的答案。 问题到底是什么? 我想运行 2 个脚本。在第一个脚本中,我调用另一个脚本,但我希望它们继续并行,而不是在两个单独的线程中。主要是我不希望第
我有一个带有 python 2.5.5 的软件。我想发送一个命令,该命令将在 python 2.7.5 中启动一个脚本,然后继续执行该脚本。 我试过用 #!python2.7.5 和http://re
我在 python 命令行(使用 python 2.7)中,并尝试运行 Python 脚本。我的操作系统是 Windows 7。我已将我的目录设置为包含我所有脚本的文件夹,使用: os.chdir("
剧透:部分解决(见最后)。 以下是使用 Python 嵌入的代码示例: #include int main(int argc, char** argv) { Py_SetPythonHome
假设我有以下列表,对应于及时的股票价格: prices = [1, 3, 7, 10, 9, 8, 5, 3, 6, 8, 12, 9, 6, 10, 13, 8, 4, 11] 我想确定以下总体上最
所以我试图在选择某个单选按钮时更改此框架的背景。 我的框架位于一个类中,并且单选按钮的功能位于该类之外。 (这样我就可以在所有其他框架上调用它们。) 问题是每当我选择单选按钮时都会出现以下错误: co
我正在尝试将字符串与 python 中的正则表达式进行比较,如下所示, #!/usr/bin/env python3 import re str1 = "Expecting property name
考虑以下原型(prototype) Boost.Python 模块,该模块从单独的 C++ 头文件中引入类“D”。 /* file: a/b.cpp */ BOOST_PYTHON_MODULE(c)
如何编写一个程序来“识别函数调用的行号?” python 检查模块提供了定位行号的选项,但是, def di(): return inspect.currentframe().f_back.f_l
我已经使用 macports 安装了 Python 2.7,并且由于我的 $PATH 变量,这就是我输入 $ python 时得到的变量。然而,virtualenv 默认使用 Python 2.6,除
我只想问如何加快 python 上的 re.search 速度。 我有一个很长的字符串行,长度为 176861(即带有一些符号的字母数字字符),我使用此函数测试了该行以进行研究: def getExe
list1= [u'%app%%General%%Council%', u'%people%', u'%people%%Regional%%Council%%Mandate%', u'%ppp%%Ge
这个问题在这里已经有了答案: Is it Pythonic to use list comprehensions for just side effects? (7 个答案) 关闭 4 个月前。 告
我想用 Python 将两个列表组合成一个列表,方法如下: a = [1,1,1,2,2,2,3,3,3,3] b= ["Sun", "is", "bright", "June","and" ,"Ju
我正在运行带有最新 Boost 发行版 (1.55.0) 的 Mac OS X 10.8.4 (Darwin 12.4.0)。我正在按照说明 here构建包含在我的发行版中的教程 Boost-Pyth
学习 Python,我正在尝试制作一个没有任何第 3 方库的网络抓取工具,这样过程对我来说并没有简化,而且我知道我在做什么。我浏览了一些在线资源,但所有这些都让我对某些事情感到困惑。 html 看起来
我是一名优秀的程序员,十分优秀!