Problem Statement: How to refer to the Null object in Python?
None in Python [A Quick Overview]
In programming languages like C or Java, generally, there is a null pointer available that can be used for different purposes. But the
null pointer is not available in Python. Generally, in Java,
null is a variable that is empty (to mark default parameters) or a pointer that does not point to anything. However, in Python, null is not characterized by 0 or empty.
text = "" if text == Null: print("No text found") # Output: NameError: name 'Null' is not defined
The bottom line is that ‘there is no Null keyword available in Python‘. However, we can use the ‘
None‘ keyword that is an object to refer to Null objects.
⦿ Python utilizes “
None” to define the null objects and variables.
None is an instance of the
None is built-in in Python and is accessible in all modules and classes.
⦿ In Python 2.4 versions or more, it isn’t possible to overwrite None.
None can be assigned to any variable, but we cannot create any other
NoneType objects. The variables assigned
None points to the same object.
⦿ Ensure that the first letter in the “
None” keyword is capital
N; otherwise, it may result in an error.
text = None if text == None: print("Null type detected! Object type : ", type(text))
Null type detected! Object type : <class 'NoneType'>
How to Check if Something is of the Type None?
The simplest way to check if something has been assigned “
None” as a value is to use the “
is” identity operator. The “
is” operator tests whether the two variables refer to the same object. It will return
True if the value assigned is
None and return
False if it’s
# Checking if the object is None using "is" operator # Non-empty object x = 2 print(x is None) # Null object y = None print(y is None) # An empty function class Empty(object): def __eq__(self, z): return not z e = Empty() print(e is None)
False True False
Caution: Another way to test whether the object is None or not is to use the comparison operators (==, != ). However, using the comparison operator is not advisable as it may also return empty objects and functions as None objects which is logically not the case. Let’s have a look at an example that demonstrates this issue.
# Checking if the object is None using "==" operator # Non-empty object x = 2 print(x == None) # Null object y = None print(y == None) # This is an empty function not a null function class Empty(object): def __eq__(self, z): return not z def foo(): pass e = Empty() # Note the difference print(e == None) print(e is None)
False True True False
Explanation: In the above example, the “
e” object must not return True as it is an empty function and not a None object. However, the comparison operator evaluates it to be
True even though it is not a None type object. Hence, it is advisable to use the “
is” operator while working with the “None”
Using “None” in List
Let’s create a list with a “
None” value in Python. We have to check if the list accepts the None value and whether this value is considered while calculating the length of the list.
# Example of a List with None values l = ['Referring', 'to', None, 'object', 'in', 'Python'] # Length of ist including None values print("The length of the list is: ", len(l)) # Printing the list values that includes "None" for i in range(0, len(l)): print("The", i + 1, "item on the list is:", l[i])
Output: The output clearly suggests that
None is also counted as an element when used in the list.
The length of the list is: 6 The 1 item on the list is: Referring The 2 item on the list is: to The 3 item on the list is: None The 4 item in the list is: object The 5 item on the list is: in The 6 item on the list is: Python
None” cannot be associated with any other list method. If we declare a list as
None, we cannot use any methods associated with the list. This is true for every other iterable object/data type.
# Simple list l1 = [5, 10, 15, 20, 25] print(l1) l1.append(30) print(l1) # None assigned to list l2 = None print(l2) l2.append(30) print(l2)
[5, 10, 15, 20, 25] [5, 10, 15, 20, 25, 30] None Traceback (most recent call last): File "<main.py>", line 10, in <module> AttributeError: 'NoneType' object has no attribute 'append'
In the above example, we can add or delete elements from list1 as list data type allows such methods in Python. However, when we try to append the element to
l1 which is assigned to
None we get an error. This happens because the
'NoneType' object has no attribute 'append'.
Using None as default argument in Python
# Default value value = 2 class Number: def check(self, x=None): # Check if default argument is None if x is None: x = value return x n = Number() print("The number is", n.check()) print("The number is", n.check(10))
The number is 2 The number is 10
Explanation: In the above code, the check method has a default argument
x that has been assigned
None as the default value. When nothing is passed to
x is assigned with the number stored in the variable
value as can be seen in the first function call. In the second function call
x gets a value of
10 which overrides its default value and the returned value is
Using Pandas to Check if the Object is None
We can also use the Panda’s module in Python to check if the object is referring to
None value. You can use the
isnull() function of the Pandas module that is used to indicate whether the values are missing from the object (Referenced to
# Importing pandas module import pandas as pd # None object x = pd.isnull(None) print(x) # Non-empty object z = pd.isnull('Python') print(z) # An empty function class Empty(object): def __eq__(self, z): return not z e = pd.isnull(Empty()) print(e == None)
True False False
That’s all about today’s discussion. Here’s what we learned today –
- What is None in Python and how it is referenced.
- None in a List
- Using None as a default value
- Pandas and None
Join us @ FINXTER COMPUTER SCIENCE ACADEMY