Python TypeError: 'NoneType' object is not callable Solution

Posted in /  

Python TypeError: 'NoneType' object is not callable Solution
vinaykhatri

Vinay Khatri
Last updated on April 25, 2024

    In Python to call a function, we use the function name followed by the parenthesis () . But if we try to call other Python objects like, int , list , str , tuple , etc., using parenthesis, we receive the TypeError Exception with an error message that the following object is not callable.

    None is a reserved keyword value in Python, which data type is NoneType . If we put parenthesis " () " after a None value and try to call it as a function we receive the TypeError: 'NoneType' object is not callable Error.

    In this Python guide, we will walk through this Python error and discuss why it occurs in Python and how to solve it. We will also discuss a common example scenario where many Python learners commit mistakes while writing the program and encounter this error.

    So let's get started with the Error Statement!

    Problem: TypeError: 'NoneType' object is not callable

    The TypeError: 'NoneType' object is not callable is a  standard Python error statement and like other error statements, it can be divided into two parts.

    1. Exception Type TypeError
    2. Error Message ( 'NoneType' object is not callable )

    1. TypeError

    TypeError is a standard Python exception, it occurs in a Python program when we perform an unsupportable operation on a specific Python object. For instance, integer objects do not support indexing in Python. And if we try to perform indexing on an integer object, we also receive the TypeError with some error message.

    2. 'NoneType' object is not callable

    "NoneType object is not callable" is an error message, and it is raised in the program when we try to call a NoneType object as a function. There is only one NoneType object value in Python None

    # None Value
    obj = None
    
    print('Type of obj is: ',type(obj))

    Output

    Type of obj is: <class 'NoneType'>

    And if we try to call the None value objects or variables as a function using parenthesis () we receive the TypeError with the message 'NoneType' object is not callable'

    Example

    # None Value
    obj = None
    
    # call None obj as a function 
    obj()

    Output

    Traceback (most recent call last):
      File "main.py", line 5, in 
        obj()
    TypeError: 'NoneType' object is not callable

    In this example, we tried to call the obj variable as a function obj() and we received the Error. The value of obj is None which make the obj a NoneType object, and we can not perform function calling on a NoneType object. When the Python interpreter read the statement obj() , it tried to call the obj as a function, but soon it realised that obj is not a function and the interpreter threw the error.

    Common Example Scenario

    The most common example where many Python learners encounter this error is when they use the same name for a function and a None Type variable. None value is mostly used to define an initial value, and if we define a function as the same name of the variable with None value we will receive this error in our Program.

    Example

    Let's create a Python function result() that accepts a list of paired tuples students [(name,marks)] and return the name and the marks of the student who get the maximum marks.

    def result(students):
        # initilize the highest_marks
        highest_marks = 0
    
        for name, marks in students:
            if marks> highest_marks:
                rank1_name = name
                highest_marks =marks
    
        return (rank1_name, highest_marks)
    
    students = [("Aditi Musunur", 973),
                ("Amrish Ilyas", 963),
                ("Aprativirya Seshan", 900),
                ("Debasis Sundhararajan", 904),
                ("Dhritiman Salim",978) ]
    
    # initial result value
    result = None
    
    # call the result function
    result = result(students)
    
    print(f"The Rank 1 Student is {result[0]} with {result[1]} marks")

    Output

    Traceback (most recent call last):
      File "main.py", line 22, in 
        result = result(students)
    TypeError: 'NoneType' object is not callable

    Break the code In this example, we are getting the error in 22 with result = result(students) statement. This is because at that point result is not a function name but a None value variable. Just above the function calling statement, we have defined the result value as None using the result = None statement.

    As Python executes its code from top to bottom the value of the result object changed from function to None when we define the result initial value after the function definition. And the Python interpreter calls the None object as a function when it interprets the result() statement.

    Solution

    The solution to the above problem is straightforward. When writing a Python program we should always consider using the different names for functions and value identifiers or variables. To solve the above problem all we need to do is define a different name for the result variable that we used to store the return value of the result function.

    def result(students):
        # initilize the highest_marks
        highest_marks = 0
    
        for name, marks in students:
            if marks> highest_marks:
                rank1_name = name
                highest_marks =marks
    
        return (rank1_name, highest_marks)
    
    students = [("Aditi Musunur", 973),
                ("Amrish Ilyas", 963),
                ("Aprativirya Seshan", 900),
                ("Debasis Sundhararajan", 904),
                ("Dhritiman Salim",978) ]
    
    # initialize the initial value
    rank1= None
    
    # call the result function
    rank1 = result(students)
    
    print(f"The Rank 1 Student is {rank1[0]} with {rank1[1]} marks")

    Output

    The Rank 1 Student is Dhritiman Salim with 978 marks

    Conclusion

    In this Python error tutorial, we discussed TypeError: 'NoneType' object is not callable Error, and learn why this error occurs in Python and how to debug it. You will only encounter this error in your Python program when you try to call a None object as a function. To debug this error you carefully need to read the error and go through the code again and look for the line which is causing this error.

    With some practice, you will be able to solve this error in your program in no time. If you are still getting this error in your Python program, you can share the code in the comment section. We will try to help you in debugging.

    People are also reading:

    Leave a Comment on this Post

    0 Comments