Python-Ref > Basic usage > Variables > Variable removal
 
 

<-^^->
Klíčová slova
Moduly
Knihovní funkce

Variable removal

How to get rid of a variable once it is used no more.
To remove a variable use the del keyword.
Expand/Shrink
Zdroj: (var2-1.py)
  1   a = 1
  2   print a
  3    
  4   del a   # we remove the variable a
  5   print a   # this will cause an error because a is not defined
stdout:
1
stderr:
Traceback (most recent call last):
  File "var2-1.py", line 6, in ?
    print a   # this will cause an error because a is not defined
NameError: name 'a' is not defined
Doba běhu: 21.2 ms
See also the example Scope of variables used inside functions for another example of situations where a variable might 'disappear'.