在python中清除变量

问题:在python中清除变量

有没有办法清除python中变量的值?

例如,如果我正在实现一棵二叉树:

Class Node:
    self.left = somenode1
    self.right = somenode2

如果要从树中删除某些节点,则需要将其设置self.left为空。

Is there a way to clear the value of a variable in python?

For example if I was implementing a binary tree:

Class Node:
    self.left = somenode1
    self.right = somenode2

If I wanted to remove some node from the tree, I would need to set self.left to empty.


回答 0

这有什么错self.left = None

What’s wrong with self.left = None?


回答 1

del关键字会做。

>>> a=1
>>> a
1
>>> del a
>>> a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined

但在这种情况下,我投赞成票 self.left = None

The del keyword would do.

>>> a=1
>>> a
1
>>> del a
>>> a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined

But in this case I vote for self.left = None


回答 2

var = None “清除值”,将变量的值设置为“ null”,就像“ None”的值一样,但是指向变量的指针仍然保留。

del var 完全删除变量的定义。

如果您以后想使用该变量,例如为其设置一个新值,即保留该变量,None会更好。

var = None “clears the value”, setting the value of the variable to “null” like value of “None”, however the pointer to the variable remains.

del var removes the definition for the variable totally.

In case you want to use the variable later, e.g. set a new value for it, i.e. retain the variable, None would be better.


回答 3

实际上,这不会删除变量/属性。它所要做的只是将其值设置为None,因此该变量仍会占用内存空间。如果要从内存中完全清除变量的所有存在,可以键入:

del self.left

Actually, that does not delete the variable/property. All it will do is set its value to None, therefore the variable will still take up space in memory. If you want to completely wipe all existence of the variable from memory, you can just type:

del self.left

回答 4

  • 如果要完全删除它,请使用del

    del your_variable

  • 否则,要使值None

    your_variable = None

  • 如果它是可迭代的(例如列表或元组),则可以将其设为空,例如:

    your_variable.clear()

然后your_variable将是空的

  • If want to totally delete it use del:

    del your_variable

  • Or otherwise, to Make the value None:

    your_variable = None

  • If it’s an iterable (such as a list or tuple), you can make it empty like:

    your_variable.clear()

Then your_variable will be empty


回答 5

是否要删除变量,不是吗?

好的,我想我有一个@bnul答案的最佳替代主意:

您可以使用以下方法删除个人名称del

del x 或者您可以将它们从globals()对象中删除:

for name in dir(): if not name.startswith('_'): del globals()[name]

这只是一个示例循环;它仅在防御性上删除不以下划线开头的名称,并假设(不是毫无道理的)假设您在解释器的开头仅使用了没有下划线的名称。如果您确实想透彻一点,则可以使用硬编码的名称列表来保留(白名单)。除了退出并重新启动解释器外,没有内置函数可以为您执行清除操作。

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

请重新考虑这个答案!

Do you want to delete a variable, don’t you?

ok, I think I’ve got a best alternative idea to @bnul answer:

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.

Please reconsider this answer!


回答 6

我使用了上面提到的一些选项:

del self.left

或使用以下方式将值设置为“无”

self.left = None

当您将值设置为None时,了解差异并放置一些异常处理程序很重要。如果您要使用模板打印条件语句的值,请说,

print("The value of the variable is {}".format(self.left))

您可能会看到变量的值显示为“变量的值为None”。因此,您必须在其中放置一些异常处理程序:

if self.left:
    #Then only print stuff

如果self.left不是 None

I used a few options mentioned above :

del self.left

or setting value to None using

self.left = None

It’s important to know the differences and put a few exception handlers in place when you use set the value to None. If you’re printing the value of the conditional statements using a template, say,

print("The value of the variable is {}".format(self.left))

you might see the value of the variable printing “The value of the variable is None”. Thus, you’d have to put a few exception handlers there :

if self.left:
    #Then only print stuff

The above command will only print values if self.left is not None