The question is “Is there a way to clear the value of a variable in python?”, and the accepted answer is to assign to that variable a value of None
.
However, assigning to a variable a value of None and deleting that variable don’t seem to be functionally identical. And even if giving the variable an empty value was the goal, wouldn't self.left = ''
(or something else) still be a more accurate solution than self.left = None
?
Two possible arguments for why self.left = None
shouldn't be considered the accepted answer for this question:
>>> a = "a"
>>> b = "b"
>>> c = "c"
>>> d = ['foo', 'bar', 'choco', 'Kiki', 'Buba']
>>> #Argument N1
>>> a = ""
>>> del b
>>> c = None
>>> type(a)
<class 'str'>
>>> a
''
>>> type(b)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'b' is not defined
>>> type(c)
<class 'NoneType'>
>>> c
>>>
>>> #Argument N2
>>> d[1] = a
>>> d
['foo', '', 'choco', 'Kiki', 'Buba']
>>> d = ['foo', 'bar', 'choco', 'Kiki', 'Buba']
>>> d[1] = b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'b' is not defined
>>> d
['foo', 'bar', 'choco', 'Kiki', 'Buba']
>>> d[1] = c
>>> d
['foo', None, 'choco', 'Kiki', 'Buba']