Clear variable in python

后端 未结 7 1382
猫巷女王i
猫巷女王i 2020-12-22 17:16

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
            


        
7条回答
  •  隐瞒了意图╮
    2020-12-22 18:12

    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

提交回复
热议问题