How to fix "‘list’ object is not callable" in Python
Tags: python, programming
Published: 2023-02-03 13:36:46

> ✋ **Update:** This post was originally published on my blog [decodingweb.dev](https://www.decodingweb.dev), where you can read the [latest version](https://www.decodingweb.dev/python-typeerror-list-object-is-not-callable-fix) for a 💯 user experience. _~reza_

The “TypeError: ‘list’ object is not callable” error occurs when you try to call a list (`list` object) as if it was a function!

Here’s what the error looks like:

```
Traceback (most recent call last):
  File "/dwd/sandbox/test.py", line 6, in 
    more_items = list(range(11, 20))
                 ^^^^^^^^^^^^^^^^^^^
TypeError: 'list' object is not callable
```

Calling a list object as if it's a callable isn't what you'd do on purpose, though. It usually happens due to a wrong syntax or overriding a function name with a list object.

Let's explore the common causes and their solutions.

## How to fix TypeError: 'list' object is not callable?

This TypeError happens under various scenarios:

1. <u>Declaring a variable with a name that's also the name of a function</u>
2. <u>Indexing a list by parenthesis rather than square brackets</u>
3. <u>Calling a method that's also the name of a property</u>
4. <u>Calling a method decorated with `@property`</u>

**Declaring a variable with a name that's also the name of a function:** A Python function is an object like any other built-in object, such as `str`, `int`, `float`, `dict`, `list`, etc.

All built-in functions are defined in the `builtins` module and assigned a global name for easier access. For instance, list refers to the `__builtins__.list` function.

That said, overriding a function (accidentally or on purpose) with any value (e.g., a `list` object) is technically possible. 

In the following example, we've declared a variable named `list` containing a list of numbers. In its following line, we try to create another list - this time by using the `list()` and `range()` functions:

```python
list = [1, 2, 3, 4, 5, 6, 8, 9, 10] 
# ⚠️ list is no longer pointing to the list function

# Next, we try to generate a sequence to add to the current list
more_items = list(range(11, 20))
# 👆 ⛔ Raises: TypeError: ‘list’ object is not callable
```

If you run the above code, Python will complain with a "TypeError: 'list' object is not callable" error because we've already assigned the `list` name to the first list object.

We have two ways to fix the issue:

1. Rename the variable `list`
2. Explicitly access the `list()` function from the builtins module (`__bultins__.list`)

The second approach isn't recommended unless you're developing a module. For instance, if you want to implement an `open()` function that wraps the built-in `open()`:

```python
# Custom open() function using the built-in open() internally
def open(filename):
     # ...
     __builtins__.open(filename, 'w', opener=opener)
     # ...
```

In almost every other case, you should always avoid naming your variables as existing functions and methods. But if you've done so, renaming the variable would solve the issue.

So the above example could be fixed like this:

```python
items = [1, 2, 3, 4, 5, 6, 8, 9, 10] 

# Next, we try to generate a sequence to add to the current list
more_items = list(range(11, 20))
```

This issue is common with function names you're more likely to use as variable names. Functions such as `vars`, `locals`, `list`, `all`, or even user-defined functions.

In the following example, we declare a variable named `all` containing a list of items. At some point, we call `all()` to check if all the elements in the list (also named `all`) are `True`:

```python
all = [1, 3, 4, True, 'hey there', 1]
# ⚠️ all is no longer pointing to the built-in function all()


# Checking if every element in all is True:
print(all(all))
# 👆 ⛔ Raises TypeError: 'list' object is not callable
```

Obviously, we get the TypeError because the built-in function `all()` is now shadowed by the new value of the `all` variable.

To fix the issue, we choose a different name for our variable:

```python
items = [1, 3, 4, True, 'hey there', 1]


# Checking if every element in all is True:
print(all(items))
# Output: True
```

⚠️ Long story short, you should never use a function name (built-in or user-defined) for your variables!

Overriding functions (and calling them later on) is the most common cause of the "TypeError: 'list' object is not callable" error. It's similar to [calling integer numbers](https://www.decodingweb.dev/python-typeerror-int-object-is-not-callable-fix) as if they're callables.

Now, let's get to the less common mistakes that lead to this error.

**Indexing a list by parenthesis rather than square brackets:** Another common mistake is when you index a list by `()` instead of `[]`. Based on Python semantics, the interpreter will see any identifier followed by a `()` as a function call. And since the parenthesis follows a list object, it's like you're trying to call a list.

As a result, you'll get the "TypeError: ‘list’ object is not callable" error.

```python
items = [1, 2, 3, 4, 5, 6]

print(items(2))
# 👆 ⛔ Raises TypeError: 'list' object is not callable
```

This is how you're supposed to access a list item:

```python
items = [1, 2, 3, 4, 5, 6]

print(items[2])
# Output: 3
```

**Calling a method that's also the name of a property:** When you define a property in a class constructor, it'll shadow any other attribute of the same name.

```python
class Book:
    def __init__(self, title, authors):
        self.title = title
        self.authors = authors

    def authors(self):
        return self.authors

book = Book('The Pragmatic Programmer', ['David Thomas', 'Andrew Hunt'])
print(book.authors())
# 👆 ⛔ Raises TypeError: 'list' object is not callable
```

In the above example, since we have a property named `authors`, the method `authors()` is shadowed. As a result, any reference to `authors` will return the property `authors`, returning a list object. And if you call this list object value like a function, you'll get the "TypeError: ‘list’ object is not callable" error.

The name `get_authors` sounds like a safer and more readable alternative:

```python
class Book:
    def __init__(self, title, authors):
        self.title = title
        self.authors = authors

    def get_authors(self):
        return self.authors

book = Book('The Pragmatic Programmer', ['David Thomas', 'Andrew Hunt'])
print(book.get_authors())
# Output: ['David Thomas', 'Andrew Hunt']
```

**Calling a method decorated with `@property` decorator:** The `@property` decorator turns a method into a “getter” for a read-only attribute of the same name. You need to access a getter method without parenthesis, otherwise you'll get a TypeError.

```python
class Book:
    def __init__(self, title, authors):
        self._title = title
        self._authors = authors

    @property
    def authors(self):
        """Get the authors' names"""
        return self._authors

book = Book('The Pragmatic Programmer', ['David Thomas', 'Andrew Hunt'])
print(book.authors())
# 👆 ⛔ Raises TypeError: 'list' object is not callable
```

To fix it, you need to access the getter method without the parentheses:

```python
book = Book('The Pragmatic Programmer', ['David Thomas', 'Andrew Hunt'])
print(book.authors)
# Output: ['David Thomas', 'Andrew Hunt']
```

Problem solved!

Alright, I think it does it! I hope this quick guide helped you fix your problem.

Thanks for reading.

**❤️ You might like:**

- [TypeError: ‘tuple’ object is not callable in Python](https://www.decodingweb.dev/python-typeerror-tuple-object-is-not-callable-fix)
- [TypeError: ‘dict’ object is not callable in Python](https://www.decodingweb.dev/python-typeerror-dict-object-is-not-callable-fix)
- [TypeError: ‘str’ object is not callable in Python](https://www.decodingweb.dev/python-typeerror-str-object-is-not-callable-fix)
- [TypeError: ‘float’ object is not callable in Python](https://www.decodingweb.dev/python-typeerror-float-object-is-not-callable-fix)
- [TypeError: 'int' object is not callable in Python](https://www.decodingweb.dev/python-typeerror-int-object-is-not-callable-fix)