- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
del
似乎有一些让我困惑的内存。请参阅以下内容:
In [1]: import math
In [2]: math.cos(0)
Out[2]: 1.0
In [3]: del math.cos
In [4]: math.cos(0)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-4-9cdcc157d079> in <module>()
----> 1 math.cos(0)
AttributeError: module 'math' has no attribute 'cos'
很好。让我们看看如果我们删除整个数学包会发生什么:
In [5]: del math
In [6]: math.cos(0)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-6-9cdcc157d079> in <module>()
----> 1 math.cos(0)
NameError: name 'math' is not defined
所以现在数学本身已经消失了,正如预期的那样。
现在让我们再次导入数学:
In [7]: import math
In [8]: math.cos(0)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-8-9cdcc157d079> in <module>()
----> 1 math.cos(0)
AttributeError: module 'math' has no attribute 'cos'
所以不知何故,交互式 python 记得即使在我们删除整个数学包并再次导入之后,math.cos 也被专门删除了。
python 将这些知识保存在哪里?我们可以访问它吗?我们可以改变它吗?
最佳答案
一个包只从磁盘读取一次,然后作为可变单例存储在内存中。第二次导入它时,您会得到与之前导入的完全相同的单例,但它仍然缺少它的 cos
。 del math
只是删除它的本地名称,它不会从 Python 整体“取消导入”包。
关于python - 包上的 `del` 有某种内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48809458/
据我所知,del a 和 del(a) 似乎都具有相同的效果。如果是这样,为什么 Python 允许 del 作为语句和函数存在? 最佳答案 del 始终是一个语句。使用括号并不意味着您在进行函数调用
我已经用 Python 创建了一个列表 >>> my_list = [1, 2, 3, 4] 现在如果我想删除列表,我想使用 del 运算符,比如 >>> del my_list 这很好用并且可能是使
我在 (xx,yy) 网格中定义了一个二维函数 f(x,y)。我想在数值上获得它的偏导数,如下所示。请注意,np.gradient 不会完成这项工作,因为它返回沿每个轴的矢量场。 我该怎么做?这是我的
我有一个在 Linux 机器上运行的服务,它创建一个 命名管道 字符设备专用文件,我想编写一个 Python3 程序,通过编写文本命令和读取文本与该服务通信来自 pipe 设备的回复。我没有该服务的源
我正在尝试此代码,但它不起作用,请帮忙,jquery 新手 我的 HTML SAM C WILL HAVE BACON
这个问题在这里已经有了答案: Why does del (x) with parentheses around the variable name work? (2 个答案) "assert" st
我有一个 TimeInterval 我必须将它变成一个 String 以便它可以添加到从 Apple Watch 传递的 Dictionary通过 WatchConnectivity 连接到 iPho
我希望我的 C/M-h 键成为删除的绑定(bind)。 global-set-key 的通常解决方案对我不起作用,因为我希望这些键在任何地方都表现为删除,包括 minibuffer 和各种模式,并且能
考虑以下代码 #! /usr/bin/env python my_dict = {1:['Bernd','das','Brot'], 2:['Chili','das','Schaf'], 3:['Br
我对字典的 del 语句有问题,因为我得到了多次删除。例如我的代码如下所示: info_dict = {'parent1':{'exon':{'exon1':{'str_0':1, 'end_0':1
for x,y in words: for z in x: if z in stopwords: del x[x.index(z)] 这是我的代码。 w
今天测试一些 Python 代码,我尝试了以下代码: (以下运行在 Python 3.2+ 上,尽管以前的版本在使用 del 并且在封闭范围内引用变量时会引发 SyntaxError) def x()
我写了一个 python 脚本,可以在晚上 sleep 时备份我的文件。该程序设计为在计算机打开时运行,并在备份完成后自动关闭计算机。我的代码如下所示: from datetime import da
我对 python 和编程非常陌生。我现在正在学习循环,并尝试了以下代码来看看会发生什么。我想知道的是为什么下面的代码不会崩溃或不断重复“Robert”。 谢谢。 name = ["Robert",
在我的 python 程序中,我使用 pandas 读取一个 csv 文件并存储在内存中: data = pandas.read_csv('data.csv') 在运行上述命令之前,我使用 free
我有一个长列表,其中每个元素都是一个长度为 2 的列表。每个列表的第一个元素是一个字符串,每个列表的第二个元素是一个与字符串对应的整数。 我想遍历长长的“父”列表并删除整数小于三的任何“子”列表。这是
class ToBeDeleted: def __init__(self, value): self.value = val # Whatever... def
假设我们有一个 python 类: class A(object): def __del__(self): print "Del!" __del__ 在任何 A 实例的删除/垃
当我尝试在 lambda 中使用 del 将线程列表精简为仅运行的线程时,我注意到了一个问题: map(lambda x: del(x) if not x.isAlive() else x, self
是否有一种单行方式可以执行以下操作? myDict = {} if 'key' in myDic: del myDic['key'] 谢谢 最佳答案 你可以写 myDict.pop(key,
我是一名优秀的程序员,十分优秀!