Is there a way to delete created variables, functions, etc from the memory of the interpreter?

2021/12/04 08:42
阅读数 0

问题:

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:当我在 Python 的 shell 中时,我输入: dir()并且我可以看到当前作用域(主块)中所有对象的所有名称,其中有 6 个:

['__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:然后,当我声明一个变量时,例如x = 10 ,它会自动添加到内置模块dir()下的对象列表中,当我再次键入dir()时,它现在显示:

['__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?如何删除所有这些新对象而不擦除开始时可用的标准 6?

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.但这一切与我想要实现的目标无关,它并没有清除所有使用过的对象。


解决方案:

参考: https://stackoom.com/en/question/1nNZz
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部