Table of Contents
1. Introduction
In python, we have different types of objects such as integers, strings, and floating-point numbers to represent data. In addition to these, we also have a NoneType object in python to represent the absence of data or to show that there is no data or object.
In this article, we will discuss NoneType in python. We will also look at some properties and applications of NoneType objects.
2. What is NoneType in Python?
In python, we have a special object None
to represent that a variable doesn’t refer to any object. The object None
has the datatype NoneType
. In other words, NoneType is the data type of the value None in python.
To visualize this, let us declare a variable with the Value None and check its data type.
1 2 3 4 |
myVal = None print("Datatype of {} is {}.".format(myVal, type(myVal))) |
Output:
1 2 3 |
Datatype of None is <class 'NoneType'>. |
Here, we can observe that any variable that has the value None will have NoneType data type in Python.
3. Properties of NoneType Objects in Python
NoneType in python has only one object i.e. None
. There can be only one None object in a python program.
Even if you declare two or more variables with the value None, they will refer to the same NoneType
object. We can verify this using the identity of the objects that we can find using the id()
function.
In python, every object has its unique identity number. The id()
function takes a variable as an input argument and returns the identity number of the variable as shown below.
1 2 3 4 |
myVal = None print("Identity Number of {} is {}.".format(myVal, id(myVal))) |
Output:
1 2 3 |
Identity Number of None is 9484816. |
We can verify that two variables that have the value None
refer to the same object by checking their identity using the id()
function as follows.
1 2 3 4 5 6 |
myVal = None myVal2 = None print("Identity Number of myVal is {}.".format(id(myVal))) print("Identity Number of myVal2 is {}.".format(id(myVal2))) |
Output:
1 2 3 4 |
Identity Number of myVal is 9484816. Identity Number of myVal2 is 9484816. |
Here, we can observe that both the variables have the same identity number, although we have declared two different variables and have assigned values to them separately. This confirms that a python program can have only one NoneType object.
As there is only one None object, we can use the identity operator “is
” instead of equality operator “==” to check if a variable has the value None. This is because the identity operator has faster execution than the equality operator. However, using the equality operator will not raise any error in your program.
Another property of NoneType
objects is that they evaluate to False if used in a boolean expression. For instance, if we use an integer object like a boolean expression in an if statement, it evaluates to True. On the other hand, NoneType
objects evaluate to False if used as a boolean expression in an if statement.
We can observe this in the following example:
1 2 3 4 5 6 7 |
myVal = None if myVal: print("value of myVal is {} and it evaluates to True".format(myVal)) else: print("value of myVal is {} and it evaluates to False.".format(myVal)) |
Output:
1 2 3 |
value of myVal is None and it evaluates to False. |
Further reading:
4. Applications of NoneType Objects in Python
- We use the
NoneType
object in a python program whenever we have no value to assign to a variable. - Uninitialized variables also refer to the value
None
. - Sometimes,
None
is used as the default value for parameters when we define a function. - Functions that do not have a return statement return the value
None
to the caller.
5. Conclusion
In this article, we have discussed NoneType objects in python. We also discussed the properties and applications of the NoneType objects. You can use the properties of the NoneType objects to use them efficiently in your programs.