How to Solve Python TypeError: ‘range’ object is not callable

by | Programming, Python, Tips

This error is a result of trying to call a range object as if it were a function. The range() method returns a range object that consists of a series of integers. This error typically occurs when overriding the reserved word range for the built-in method. You can solve this error by not using reserved words for functions or methods that you want to use in your program.

This tutorial will go through how to solve the error with code examples.


TypeError: ‘range’ object is not callable

Calling a function means the Python interpreter executes the code inside the function. In Python, we can only call functions. We can call functions by specifying the name of the function we want to use followed by a set of parentheses, for example, function_name(). Let’s look at an example of a working function that returns a string.

# Declare function

def simple_function():

    print("Learning Python is fun!")

# Call function

simple_function()
Learning Python is fun!

We declare a function called simple_function in the code, which prints a string. We can then call the function, and the Python interpreter executes the code inside simple_function().

range objects do not respond to a function call because they are not functions. If you try to call a range object as if it were a function, you will raise the TypeError: ‘range’ object is not callable.

We can check if an object is callable by passing it to the built-in callable() method. If the method returns True, then the object is callable. Otherwise, if it returns False, the object is not callable. Let’s look at evaluating a range object with the callable() method:

val = range(1, 10, 2)

print(type(val))

print(callable(val))
<class 'range'>
False

The callable functions False for the range object.

Example

Let’s look at an example of attempting to call a range object. First, we will call the range method to get a range object containing integers between 1 and 9 with a step of 2.

range = range(1, 10, 2)

Next, we will use a for loop to iterate over the range object and print the integers to the console.

for i in range:

    print(i)

Let’s run the code to see the result:

1
3
5
7
9

Next, we will try to create another range object by calling the range() method, converting the object to a list and printing it to the console.

val = range(20)

print(list(val))

Let’s run the code to see what happens:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [28], in <cell line: 1>()
----> 1 val = range(20)
      3 print(list(val))

TypeError: 'range' object is not callable

The error occurs because we named the first range object “range“, which overrides the reserved name range for the built-in method.

Therefore, when we try to call the range() method again, we are instead trying to call the range object, which is not callable.

Solution

We can solve the error by giving the range object a different name. First, we will delete the range object using the del keyword.

del range

Then, we will recreate the range object with the variable name “range_obj“, which we can iterate over and print the values to the console.

range_obj = range(1, 10, 2)

for i in range_obj:

    print(i)
1
3
5
7
9

Next, we can define a second range object, without raising the TypeError.

val = range(20)
print(list(val))

Let’s run the code to confirm the result:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

Summary

Congratulations on reading to the end of this tutorial!

For further reading on not callable TypeErrors, go to the articles:

To learn more about Python, specifically for data science and machine learning, go to the online courses page on Python.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee ✨