Clear variable in python


Question

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.

1
181
3/30/2015 8:22:56 AM

Accepted Answer

What's wrong with self.left = None?

143
6/6/2019 11:13:03 PM

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

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


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon