Search code examples
pythoncollectionsgarbage

Garbage collection None - del difference


Is there any logical difference between a = None and del a in python?

The None keyword is used to define a null variable or an object. In python, None keyword is an object, and it is a data type of the class NoneType .

The del keyword is used to delete objects.

Are those identical to each other if I use the manual garbage collection?


Solution

  • No, None is still an object (of type NoneType) that takes memory and can be referenced whereas del deletes the object's name from the name space.

    Say, you create a variable a = None in the global scope. It will exists in globals(). Now if you were to delete it with del a, the reference to a ceases to exist.