有没有办法从解释器的内存中删除创建的变量、函数等? [英] Is there a way to delete created variables, functions, etc from the memory of the interpreter?

查看:26
本文介绍了有没有办法从解释器的内存中删除创建的变量、函数等?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天来我一直在寻找这个问题的准确答案,但没有得到任何好的答案.我不是一个完整的编程初学者,但还没有达到中级水平.

当我在 Python 的 shell 中时,我输入:dir() 并且我可以看到当前作用域(主块)中所有对象的所有名称,有 6 个他们:

['__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__']

然后,当我声明一个变量时,例如 x = 10,它会自动添加到内置模块 dir() 下的对象列表中,当我再次输入 dir() 时,它现在显示:

['__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'x']

函数、类等也是如此.

如何删除所有这些新对象而不删除最初可用的标准 6?

我在这里阅读了有关内存清理"、控制台清理"的内容,它们会从命令提示符窗口中删除所有文本:

<预><代码>>>>导入系统>>>clear = lambda: os.system('cls')>>>清除()

但这一切都与我要实现的目标无关,它并没有清除所有使用过的对象.

解决方案

您可以使用 del 删除单个名称:

del x

或者你可以从 globals() 对象中删除它们:

用于 dir() 中的名称:如果不是 name.startswith('_'):del globals()[名称]

这只是一个示例循环;它防御性地仅删除不以下划线开头的名称,从而做出(并非没有道理的)假设您只在解释器的开头使用没有下划线的名称.如果您真的想要彻底,您可以使用硬编码的名称列表来代替(白名单).除了退出并重新启动解释器之外,没有内置函数可以为您进行清除.

您导入的模块 (import os) 将保持导入状态,因为它们被 sys.modules 引用;后续导入将重用已导入的模块对象.您只是不会在当前的全局命名空间中引用它们.

然而,Python 不对内存中的数据做任何安全保证.当对象不再被引用时,解释器将内存标记为不再使用,但不会采取措施覆盖该内存以防止访问数据.如果您需要这种级别的安全保护,则需要使用第三方扩展程序来管理自己的内存并考虑到安全性.

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 module dir(), and when I type dir() 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 by sys.modules; subsequent imports will reuse the already imported module object. You just won't have a reference to them in your current global namespace.

Python doesn’t make any security guarantees about data in memory however. When objects no longer are referenced the interpreter marks the memory as no longer in use but does not take steps to overwrite that memory to prevent access to data. If you need that level of security protection you’ll need to use third-party extensions that manage their own memory with security in mind.

这篇关于有没有办法从解释器的内存中删除创建的变量、函数等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆