In Python, None
is a built-in object that represents the absence of a value. It is the sole instance of the NoneType
data type. If a variable or attribute is assigned a value of None
, it indicates that the variable or attribute has no value or has not yet been assigned a meaningful value.
A function that does not return any value typically returns None
.
print(type(None))
def func():
pass
print( func() )
None
in Python is similar to null
in other programming languages such as C++, in that they both represent absence of a value.
There is only one None
object. Even if you declare more than one object with a value of None
, they will all reference the same object. We can confirm this by using the builtin id()
function which returns the memory address of the value held by an object in the memory.
a = None
b = None
print( id(a) == id(b) )
The above example shows that both a
and b
points to the same memory location.
None
as a Boolean
The None
object has a boolean value of False
.
print(bool(None))
Check whether an object is None
The is
keyword can be used to check whether an object has a value of None
.
a = None
b = 1
print(a is None)
print(b is None)
While we can also use the equality operator ( ==
) to check whether an object has a value of None
, the identity operator ( is
), as used above, is more appropriate and efficient because there is only one None
object.