How to fix "‘int’ object is not callable" in Python
Tags: python, programming
Published: 2023-02-03 14:37:06

> ✋ **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-int-object-is-not-callable-fix) for a 💯 user experience. _~reza_

The “TypeError: ‘int’ object is not callable” error occurs when you try to call an integer (`int` 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 4, in 
  round = round(result)
          ^^^^^^^^^^^^^
TypeError: 'int' object is not callable
```

In the simplest terms, this is what happens:

```python
result = 33 / 5
round = round(result)
# ⚠️ The value of round is 7 from now on 
# it's no longer pointing to the round() built-in function

result = 34 / 8

# ⛔ Calling round at this point is equivalent to calling 7()
round = round(result)
```

Additionally, if you put an extra parenthesis after a function such as `round()`, you'll get the same TypeError:

```python
print(round(14.5)())
```

The reason is `round()` function returns an `int` object, and having an extra pair of parenthesis means calling the returned value. 

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

This error happens under various scenarios:

1. <u>Declaring variable with a name that's also the name of a function</u>
2. <u>Calling a method that's also the name of a property</u>
3. <u>Calling a method decorated with `@property`</u>
4. <u>Missing a mathematical operator</u>

Let's explore each scenario with some examples.

**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 `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, `sum()` refers to the `__builtins__.sum()` function.

That said, overriding a function (accidentally or on purpose) with another value is technically possible. 

For instance, if you define a variable named `sum` and initialize it with an integer value, sum will no longer point to the `sum` class.

```python
# this overrides the sum value to 0
sum = 0
values = [34, 43, 2, 8, 1]

# ⛔ Raises TypeError: 'int' object is not callable
sum = sum(values)
```

If you run the above code, Python will complain with a "TypeError: 'int' object is not callable" error because `0` (the new value of sum) isn't callable.

You have two ways to fix the issue:

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

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
sum_of_values = 0
values = [34, 43, 2, 8, 1]

sum_of_values = sum(values)
print(sum_of_values)
# output: 88
```

Here's another example with the built-in `max()` function:

```python
max = 0
items = [1, 45, 54, 165, 0, 2]

# ⛔ Raises "TypeError: 'int' object is not callable"
max = max(items)
```

And to fix it, we rename the `max` variable name to `max_value`:

```python
max_value = 0
items = [1, 45, 54, 165, 0, 2]

max_value = max(items)
print('The biggest number is:', max_value)
# output: The biggest number is: 165
```

⚠️ 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: 'int' object is not callable" error. 

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

**Calling a method that's also the name of a property:** When you define a property in a class constructor, any further declarations of the same name (e.g., methods) will be ignored.

```python
class Book:
    def __init__(self, book_code, book_title):
        self.title = book_title
        self.code = book_code

    def code(self):
        return self.code

book = Book(1, 'Head First Python')

# ⛔ Raises "TypeError: 'int' object is not callable"
print(book.code())
```

In the above example, since we have a property named `code`, the method `code()` is ignored. As a result, any reference to the `code` will return the property `code`. Obviously, calling `code()` is like calling `1()`, which raises the "TypeError: 'int' object is not callable" error.

To fix this TypeError, we need to change the method name:

```python
class Book:
    def __init__(self, book_code, book_title):
        self.title = book_title
        self.code = book_code

    def get_code(self):
        return self.code

book = Book(1, 'Head First Python')
print(book.get_code())
```

**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.

```python
class Book:
    def __init__(self, book_code, book_title):
        self._title = book_title
        self._code = book_code

    @property
    def code(self):
        """Get the book code"""
        return self._code

book = Book(1, 'Head First Python')

# ⛔ Raises "TypeError: 'int' object is not callable"
print(book.code())
```

You need to access the getter method without the parentheses:

```python
book = Book(1, 'Head First Python')

print(book.code)
# Output: 1
```

**Missing a mathematical operator:** In algebra, we can remove the multiplication operator to avoid ambiguity in our expressions. For instance, `a × b`, can be `ab`, or `a × (b + c)` can become `a(b + c)`.

But not in Python!

In the above example, if you remove the multiplication operator in `a * (b + c)`, Python's interpreter would consider it a function call! And since the value of `a` is numeric (integer in this case), it'll raise the "TypeError: 'int' object is not callable" error.

So if you have something like this in your code:

```python
a = 12
b = 3
c = 6

# ⛔ raises  TypeError: 'int' object is not callable
result = a (b + c)
```

You'd have to change it like so:

```python
a = 12
b = 3
c = 6

result = a * (b + c)

print(result)
# output: 108
```

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: can only concatenate str (not “float”) to str (solutions)](https://www.decodingweb.dev/python-typeerror-can-only-concatenate-str-not-float-to-str-fix)
- [TypeError: can only concatenate str (not “int”) to str (Solutions)](https://www.decodingweb.dev/python-typeerror-can-only-concatenate-str-not-int-to-str-fixed)
- [TypeError: can only concatenate str (not "bool") to str (Fixed)](https://www.decodingweb.dev/python-typeerror-can-only-concatenate-str-not-bool-to-str-fix)
- [TypeError: can only concatenate str (not "dict") to str (Fixed)](https://www.decodingweb.dev/python-typeerror-can-only-concatenate-str-not-dict-to-str-fix)
- [TypeError: can only concatenate str (not "list") to str (Fixed)](https://www.decodingweb.dev/python-typeerror-can-only-concatenate-str-not-list-to-str-fix)