Typeerror First Argument Must be Callable or None

The Typeerror: first argument must be callable or none error occurs when we used non-callable object as an argument to the function. The error indicates that the argument that we have passed to the function is not callable. This short article will discuss how we can solve Typeerror: first argument must be callable or none error using various methods. Moreover, we will also cover how to interpret and understand errors in Python.

If you are interested in learning a full course on Machine Learning, please have a look at our free machine learning course with projects and assignments.

Solve first argument must be callable or none Error

We know the errors in any programming language can be a headache. The error occurs when we pass a non-callable object as an argument to the function when the function needs a callable argument. For example, the input() method is a callable method and the following code will return true.

print(callable(input))

This will return True because the input() in Python is a callable object.

Let us take another example. Let us assume that you are using a function that takes the first argument as a callable object.

# importing the defaultdic
from collections import defaultdict

# creating a list
l = list(range(0,10))

# using default dict which takes first argument as callable object
d = defaultdict(lst)

Output:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_18036/2585466598.py in <module>
      6 
      7 # using default dict which takes first argument as callable object
----> 8 d = defaultdict(lst)

TypeError: first argument must be callable or None

The reason for getting this error is that the defaultdict() method takes the first argument as a callable object and we provide a list which is not a callable.

The error can be solved using the following methods:

  1. Using a callable object
  2. Using a none type
  3. using try-except block

Let us go through each of the methods in detail.

Solve Using a callable object

The simplest method to get rid of this error is to use the callable object as the argument to the function.

There are many methods, we can use in order to use a callable object. For demonstration purposes, we will use the previous example and see how we can pass a callable object to the function. You can use your own example accordingly.

The first way is to use the lambda function to convert the list into a callable object. lambda is a keyword in Python for defining the anonymous function. argument(s) is a placeholder, which is a variable that will be used to hold the value you want to pass into the function expression. See the example below:

# importing the defaultdic
from collections import defaultdict

# using the lambda to create a function
l = lambda:list(range(0,5))

# calling by argument
d = defaultdict(l)

When you run the code, it will execute without giving any error which means the first argument this time is a callable object.

Or you can also use all in one line and create the lambda function as the first argument to the function:

# collection import
from collections import defaultdict

# callable argument
d = defaultdict(lambda: list(range(0,5)))

Hopefully, this will help you to get rid of the error.

Solve Using None object

Another way to get rid of the error is to use None object as an argument rather than a callable object. Let us see the example below:

# importing the defaultdic
from collections import defaultdict

# calling by argument
d = defaultdict(None)

When you run the code, it will not return any error because the None object will be used as an argument.

Solve Using try-except blocks

If you are not sure what type of argument will be and you don’t know. In such cases, you can use the try-except block to handle the error. The code inside the try block will be executed first and if any error occurs then the except block will be executed.

# importing the defaultdic
from collections import defaultdict

# using the  to create a function
l = list(range(0,5))

# try block
try:
    # calling b
    d = defaultdict(lst)
    
# except
except:
    print("Error occurs in the try block")

Output:

Error occurs in the try block

This shows that there was an error in the try block but instead of crashing the script, the except block was executed.

Conclusion

We discussed how we can solve the error using various methods. We discussed three different methods to solve the error along with examples. Moreover, we also learned how to interpret and understand errors in Python.

Other Issues

Leave a Comment