有没有办法从解释器的内存中删除创建的变量,函数等? [英] Is there a way to delete created variables, functions, etc from the memory of the interpreter?
问题描述
我一直在寻找这个问题的准确答案几天,但没有什么好处。我不是一个完整的编程初学者,但还没有在中级。
当我在Python的shell中时,我键入: dir()
,我可以看到当前范围(主要块)中所有对象的所有名称,其中有6个:
['__ builtins__','__doc__','__loader__','__name__','__package__','__spec__']
/ pre>
然后,当我声明一个变量时,例如
x = 10
,它会自动添加到内置模块dir()
中的对象列表,当我再次键入dir()
时,它显示现在:['__ builtins__','__doc__','__loader__','__name__','__package__','__spec__' ,'x']
同样的功能,类等。
如何删除所有这些新对象而不删除标准6哪里可用的开始?
我在这里阅读过关于内存清理,清理控制台的内容,它从命令提示符窗口中删除所有文本:
>>>导入sys
>>>> clear = lambda:os.system('cls')
>>> clear()
但这一切与我想要实现的无关,它没有
解决方案您可以删除具有
del $ c的个人名称$ c>
del x
,或者您可以从
globals()
对象中删除它们:在dir()中的名称:
如果不是name.startswith('_'):
del globals()[name]
这只是一个例子循环;防御性地只删除不以下划线开始的名称,作出(不是未经验证的)假设,您只能在解释器的开头使用没有下划线的名称。您可以使用硬编码的名称列表来保持(白名单),如果你真的想彻底。除了退出并重新启动解释器之外,没有内置函数来执行清除。
您导入的模块(
导入os
)将保留导入,因为它们由sys.modules
引用;后续导入将重用已导入的模块对象。你不会在当前的全局命名空间中引用它们。I've been searching for the accurate answer to this question for a couple of days now but haven't got anything good. I'm not a complete beginner in programming, but not yet even on the intermediate level.
When I'm in the shell of Python, I type:
dir()
and I can see all the names of all the objects in the current scope (main block), there are 6 of them:['__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__']
Then, when I'm declaring a variable, for example
x = 10
, it automatically adds to that lists of objects under built-in moduledir()
, and when I typedir()
again, it shows now:['__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'x']
The same goes for functions, classes and so on.
How do I delete all those new objects without erasing the standard 6 which where available at the beginning?
I've read here about "memory cleaning", "cleaning of the console", which erases all the text from the command prompt window:
>>> import sys >>> clear = lambda: os.system('cls') >>> clear()
But all this has nothing to do with what I'm trying to achieve, it doesn't clean out all used objects.
解决方案You can delete individual names with
del
:del x
or you can remove them from the
globals()
object:for name in dir(): if not name.startswith('_'): del globals()[name]
This is just an example loop; it defensively only deletes names that do not start with an underscore, making a (not unreasoned) assumption that you only used names without an underscore at the start in your interpreter. You could use a hard-coded list of names to keep instead (whitelisting) if you really wanted to be thorough. There is no built-in function to do the clearing for you, other than just exit and restart the interpreter.
Modules you've imported (
import os
) are going to remain imported because they are referenced bysys.modules
; subsequent imports will reuse the already imported module object. You just won't have a reference to them in your current global namespace.这篇关于有没有办法从解释器的内存中删除创建的变量,函数等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!