. Advertisement .
..3..
. Advertisement .
..4..
In Python, if you call such objects like list, int, typle, and str using parenthesis, you will likely receive a TypeError Exception error message: ‘NonType’ Object Is Not Callable.
So what is the problem? How to solve it? Here is the answer.
What Is The Problem?
The TypeError: ‘NonType’ object is not callable is a standard error statement. There are two parts in this error: exception type and error message.
TypeError occurs when a Python object runs an unsupportable operation. For example, indexing is not supported by integer objects. So when running indexing on this type of object, we will receive this error message.
The ‘NoneType’ object is not callable error message raises when you call a NoneType object as a function. Python has only one NoneType object value named None.
Overall, the problem occurs when you call this ‘None’ value as a function. That’s why you need to track down its root cause and remove the parentheses or correct the assignment to solve the problem.
# None Value
obj = None
print('Type of obj is: ',type(obj))
Output:
Type of obj is: <class 'NoneType'>
You can accidentally function the objects and variables using parenthesis() with the same name. So if you use the same name for a NoneType variable and a function, the error will occur. Let’s look at the following example:
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 = [("Caroline", 973),
("David", 963),
("Mark", 900),
("Daniel", 904),
("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
How To Solve The Error
The solution for this TypeError issue is always straightforward for both beginners and professional coders. It is vital to use different names for value identifiers and functions to write a Python program.
This is also the key for the error. All you need is a new name for the result variable which contains the result function’s return value:
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 = [("Caroline", 973),
("David", 963),
("Mark", 900),
("Daniel", 904),
("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 Salim with 978 marks
Conclusion
The article has discussed the TypeError: ‘NonType’ Object Is Not Callable In Python. Now that you must have acknowledged the error, its cause, and the best solution to debug it.
Leave a comment