How To Fix “TypeError: ‘nonetype’ object is not callable” in Python

TypeError: ‘nonetype’ object is not callable

“TypeError: ‘NoneType’ object is not callable” is a common python error that shows up in many ways. What is the cause of it and how to solve it? Let’s follow our article, and we will help you.

What is the cause of this error?

When you attempt to call an object with a None value, such as a function, you will get the TypeError: “NoneType” object is not callable error message. Function calls only have responses from functions.

Example code 1: Attribute and Method have the same name

class Student:
    def __init__(obj):
        obj.fullname = None

    def fullname(obj):
        return obj.fullname # Same name with the method
		
def printMsg():
    print('Welcome to LearnShareIT')
    
def msgStudent(name, function):
    print("Hi", name)
    function() 
	
exStudent = Student()
exStudent.fullname() # TypeError: 'NoneType' object is not callable

Error Message

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-65eab4cbbd19> in <module>
     14 
     15 exStudent = Student()
---> 16 exStudent.fullname()

TypeError: 'NoneType' object is not callable

Example code 2: Misunderstand when calling a function

class Student:
    def __init__(obj):
        obj.fullname = None

    def getFullName(obj):
        return obj.fullname
		
def printMsg():
    print('Welcome to LearnShareIT')
    
def msgStudent(name, function):
    print("Hi", name)
    function() # Calling None object as a function 
	
exStudent = Student()
exStudent.fullname = "Robert Collier"

msgStudent(exStudent.getFullName(), printMsg()) # TypeError: 'NoneType' object is not callable

Error Message

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-16-618076368e04> in <module>
     16 exStudent.fullname = "Robert Collier"
     17 
---> 18 msgStudent(exStudent.getFullName(), printMsg())
<ipython-input-16-618076368e04> in msgStudent(name, function)
     11 def msgStudent(name, function):
     12     print("Hi",name)
---> 13     function() # Calling None object as a function
     14 
     15 exStudent = Student()

TypeError: 'NoneType' object is not callable

Solutions to fix “TypeError: ‘nonetype’ object is not callable”

To solve this error, let’s follow two solutions we suggest below:

1) Change the name of method in class

The reason of this error is that when you name the function as the name of a attribute, the attribute will be the first priority to access. As a result, you call the attribute as a function, which cause the Error as the example.

You can change the method name: fullname(obj) to getFullName(obj) as shown the code in the below example to fix the error.

Code sample

class Student:
    def __init__(obj):
        obj.fullname = None

    def getFullName(obj):
        return obj.fullname
		
exStudent = Student()
exStudent.fullname = "Robert Collier"
print(exStudent.getFullName())

Output

'Robert Collier'

2) Remove the parentheses ‘()’

When you pass a non-returning function as a parameter of another function, it is a common mistake to add the parenthesis after the function’s name. It means you call the result of the function as a function accidentally.

Remove the parentheses ‘()’ in the msgStudent(exStudent.getFullName(), printMsg()) statement. The alternative statement would be msgStudent(exStudent.getFullName(), printMsg). When you do that, your error will be fixed.

Code sample

class Student:
    def __init__(obj):
        obj.fullname = None

    def getFullName(obj):
        return obj.fullname
		
def printMsg():
    print('Welcome to LearnShareIT')
    
def msgStudent(name, function):
    print("Hi", name)
    function() # Calling None object as a function 
	
exStudent = Student()
exStudent.fullname = "Robert Collier"

msgStudent(exStudent.getFullName(), printMsg) # Remove ... ()

Output:

Hi Robert Collier
Welcome to LearnShareIT

Summary

In conclusion, “TypeError: ‘nonetype’ object is not callable” occurs when you call a None object as a function. The only way to solve the problem is understanding which parameter is an object and which parameter is a function.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *