如何清除 Python 脚本中间的所有变量? [英] How do I clear all variables in the middle of a Python script?

查看:34
本文介绍了如何清除 Python 脚本中间的所有变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Matlab 中寻找类似于clear"的东西:一个命令/函数,它从工作区中删除所有变量,从系统内存中释放它们.Python中有这种东西吗?

我想编写一个脚本,在某些时候清除所有变量.

解决方案

以下命令序列确实会从当前模块中删除每个名称:

<预><代码>>>>导入系统>>>sys.modules[__name__].__dict__.clear()

我怀疑您是否真的想要这样做,因为每个名称"都包括所有内置插件,因此在彻底清除之后您无能为力.请记住,在 Python 中确实没有变量"这样的东西——有多种类型的对象(包括模块、函数、类、数字、字符串等),并且有是名称,绑定到对象;序列所做的是从模块中删除每个名称(当且仅当对它们的每个引用都刚刚被删除时,相应的对象才会消失).

也许您想要更有选择性,但除非您想更具体,否则很难准确猜测您的意思.但是,举个例子:

<预><代码>>>>导入系统>>>this = sys.modules[__name__]>>>对于 dir() 中的 n:...如果 n[0]!='_': delattr(this, n)...>>>

这个序列只留下私有或魔法名称,包括包含所有内置名称的 __builtins__ 特殊名称.因此,内置函数仍然有效——例如:

<预><代码>>>>目录()['__builtins__', '__doc__', '__name__', '__package__', 'n']>>>

如您所见,名称 n(该 for 中的控制变量)也碰巧保留了下来(因为它在 for 中重新绑定)code> 子句每次通过),因此最好将该控制变量命名为 _,例如,清楚地显示它很特别"(另外,在交互式解释器中,命名 _ 无论如何都会在提示符处输入每个完整表达式后重新绑定到该表达式的值,因此它不会停留很长时间;-)

无论如何,一旦您确定了您想要做什么,就可以为此目的定义一个函数并将其放入您的启动文件中(如果您只需要它)在交互式会话中)或站点自定义文件(如果您希望在每个脚本中都有它).

I am looking for something similar to 'clear' in Matlab: A command/function which removes all variables from the workspace, releasing them from system memory. Is there such a thing in Python?

EDIT: I want to write a script which at some point clears all the variables.

解决方案

The following sequence of commands does remove every name from the current module:

>>> import sys
>>> sys.modules[__name__].__dict__.clear()

I doubt you actually DO want to do this, because "every name" includes all built-ins, so there's not much you can do after such a total wipe-out. Remember, in Python there is really no such thing as a "variable" -- there are objects, of many kinds (including modules, functions, class, numbers, strings, ...), and there are names, bound to objects; what the sequence does is remove every name from a module (the corresponding objects go away if and only if every reference to them has just been removed).

Maybe you want to be more selective, but it's hard to guess exactly what you mean unless you want to be more specific. But, just to give an example:

>>> import sys
>>> this = sys.modules[__name__]
>>> for n in dir():
...   if n[0]!='_': delattr(this, n)
... 
>>>

This sequence leaves alone names that are private or magical, including the __builtins__ special name which houses all built-in names. So, built-ins still work -- for example:

>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'n']
>>> 

As you see, name n (the control variable in that for) also happens to stick around (as it's re-bound in the for clause every time through), so it might be better to name that control variable _, for example, to clearly show "it's special" (plus, in the interactive interpreter, name _ is re-bound anyway after every complete expression entered at the prompt, to the value of that expression, so it won't stick around for long;-).

Anyway, once you have determined exactly what it is you want to do, it's not hard to define a function for the purpose and put it in your start-up file (if you want it only in interactive sessions) or site-customize file (if you want it in every script).

这篇关于如何清除 Python 脚本中间的所有变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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