How to Fix TypeError: 'str' Object Is Not Callable in Python

How to Fix TypeError: 'str' Object Is Not Callable in Python

The Python TypeError: 'str' object is not callable occurs when you try to call a string value (str object) as if it was a function.

TypeError: 'str' object is not callable is thrown in Python when a string value is called as if it was a function. For example, when str is declared as a variable:

str = 'I am ' # ⚠️ str is no longer pointing to a function
age = 25

# ⛔ Raises TypeError: 'str' object is not callable
print(str + str(age))

Traceback (most recent call last):
  File "/tmp/sandbox/test.py", line 5, in 
    print(str + str(age))
                ^^^^^^^^
TypeError: 'str' object is not callable

The most common solution is to rename the str variable:

text = 'I am '
age = 25

print(text + str(age))
# Output: I am 25

Calling a string value isn’t something you’d do on purpose, though. It usually happens due to a wrong syntax or accidentally overriding a function name with a string value.

What Causes the TypeError: 'str' Object Is Not Callable?

The TypeError: 'str' object is not callable error mainly occurs when:

  • You pass a variable named str as a parameter to the str() function. - as in example above
  • When you call a string like a function - declaring a variable with a name that's also the name of a function.
  • Calling a method decorated with @property.

Declaring a Variable With a Name That’s Also the Name of a Function

In the following example, we declare a variable named len. And at some point, when we call len() function to check the input, we’ll get an error:

len = '' # ⚠️ len is set to an empty string
name = input('Input your name: ')

# ⛔ Raises TypeError: 'str' object is not callable
if (len(name)):
print(f'Welcome, {name}')

To fix the issue, all we need is to choose a different name for our variable:

length = ''
name = input('Input your name: ')

if (len(name)):
print(f'Welcome, {name}')

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, such as methods, will be ignored.

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

    def title(self):
        return self.title

book = Book('Head First Python')

# ⛔ Raises "TypeError: 'str' object is not callable"
print(book.title())

In the above example, since we have a property named title, the method title() is ignored. As a result, any reference to title will return the property (a string value). And if you call title(), you’re actually trying to call a string value.

The name get_title for function sounds like a safer and more readable alternative:

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

    def get_title(self):
        return self.title

book = Book('Head First Python')

print(book.get_title())

# Output: 
Head First Python

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.

class Book:
    def __init__(self, title):
        self._title = title

    @property
    def title(self):
        """Get the book price"""
        return self._title

book = Book('Head First Python')

# ⛔ Raises "TypeError: 'str' object is not callable"
print(book.title())

You need to access the getter method without the parentheses:

class Book:
    def __init__(self, title):
        self._title = title

    @property
    def title(self):
        """Get the book price"""
        return self._title

book = Book('Head First Python')
print(book.title)

# Output: 
Head First Python

Problem solved...

Summary

In this article, we talked about the TypeError: 'str' object is not callable error in Python. To avoid getting this error in your code, you should:

  • Avoid naming your variables after keywords built into Python.
  • Never call your variables like functions by adding parentheses to them.

SUBSCRIBE FOR NEW ARTICLES

@
comments powered by Disqus