What is the None keyword in Python?
None has a special status in Python. The None is used to define a null variable or an object, and it is a data type of the class NoneType .
None is the sole instance of the class NoneType and any further attempts at instantiating that class will return the same object, which makes None a singleton (meaning there is only one None). It's a favourite baseline value because many algorithms treat it as an exceptional value, used in many places in the language and library to represent the absence of some other value .
Checking if a variable is None using is operator:
So, let's check how Python None implemented internally.
- Except __setattr__, all others are read-only attributes. So, there is no way we can alter the attributes of None.
Checking if a variable is None using == operator:
Python Null and None Keyword
There's no null in Python ; instead there's None. You can check None's uniqueness with Python's identity function id(). It returns the unique number assigned to an object, each object has one. If the id of two variables is the same, then they point in fact to the same object .
Comparing None with False type:
Interesting Facts about Python None
- None is an instance of NoneType.
- We cannot create other instances of NoneType.
- None is not the same as False.
- None cannot have new attributes.
- Existing attributes of None cannot be changed.
- None is not an empty string.
- None is not 0.
- If the function doesn't return anything that means its None in Python.
- We cannot even change the reference to None by assigning values to it.
- Comparing None to anything will always return False except None itself.