✋ Update: This post was originally published on my blog decodingweb.dev, where you can read the latest version for a 💯 user experience. ~reza
The “TypeError: ‘float’ object is not callable” error occurs when you try to call a floating-point number (float
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 8, in
sum += sum(values) # we're calling 88.6()
^^^^^^^^^^^
TypeError: 'float' object is not callable
Calling a floating-point number 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 floating-point number.
Let's explore the common causes and their solutions.
How to fix TypeError: 'float' object is not callable?
This TypeError happens under various scenarios:
- Declaring a variable with a name that's also the name of a function
- Calling a method that's also the name of a property
- Calling a method decorated with
@property
- Missing a mathematical operator after a floating-point number
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 a floating-point number is technically possible.
For instance, if you define a variable named sum
and assign it to the value of 88.6
, it'll no longer point to __builtins__.sum()
.
values = [34, 43.5, 2, 8.1, 1]
sum = sum(values) # ⚠️ The value of sum is 88.6 from now on
# it's no longer pointing the built-in function sum
values = [34, 12]
# ⛔ Raises TypeError: 'float' object is not callable
sum += sum(values) # we're calling 88.6()
If you run the above code, Python will complain with a "TypeError: 'float' object is not callable" error because 88.6
(the new value of sum
) isn't callable.
You have two ways to fix the issue:
- Rename the variable
sum
- 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()
:
# 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:
values = [34, 43.5, 2, 8.1, 1]
sum_of_values = sum(values)
values = [34, 12]
sum_of_values += sum(values)
print(sum_of_values)
# Output: 134.6
Here's another example with the built-in max()
function:
items = [1, 45, 54, 165.3, 0, 2]
max = max(items) # max = 165.3
# ...
# ⛔ Raises "TypeError: 'float' object is not callable"
print(max(12, max))
And to fix it, we rename the max
variable name to max_value
:
items = [1, 45, 54, 165.3, 0, 2]
max_value = max(items) # max = 165.3
# ...
print(max(12, max_value))
# Output: 165.3
Another common reason is accidentally overriding the range()
function with a floating-point value before using it in a for
loop:
range = 34.5
# some code here
# ⛔ Raises "TypeError: 'float' object is not callable"
for i in range(0, 10):
print(i)
To fix it, we rename the range variable:
range_start = 34.5
# some code here
for i in range(0, 10):
print(i)
⚠️ 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 this type error. It's similar to calling integer numbers as if they're callables.
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.
class Book:
def __init__(self, book_title, book_price):
self.title = book_title
self.price = book_price
def price(self):
return self.price
book = Book('Head First Python', 49.5)
# ⛔ Raises "TypeError: 'float' object is not callable"
print(book.price())
In the above example, since we have a property named price
, the method price()
is ignored. As a result, any reference to the price
will return the property price
. Obviously, calling price()
is like calling 49.5()
, which raises the type error.
To fix this TypeError, we need to change the method name:
class Book:
def __init__(self, book_title, book_price):
self.title = book_title
self.price = book_price
def get_price(self):
return self.price
book = Book('Head First Python', 49.5)
print(book.get_price())
# Output: 49.5
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, book_title, book_price):
self._title = book_title
self._price = book_price
@property
def price(self):
"""Get the book price"""
return self._price
book = Book('Head First Python', 49.5)
# ⛔ Raises "TypeError: 'float' object is not callable"
print(book.price())
You need to access the getter method without the parentheses:
book = Book('Head First Python', 49.5)
print(book.price)
# Output: 49.5
Missing a mathematical operator after a float variable: 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 (a floating-point number in this case), it'll raise the error.
So if you have something like this in your code:
a = 12.5
b = 12
c = 87
# ⛔ raises TypeError: 'float' object is not callable
result = a (b + c)
You'd have to change it like so:
a = 12.5
b = 12.3
c = 34.7
result = a * (b + c)
print(result)
# Output: 587.5
Problem solved!
Alright, I think it does it! I hope this quick guide helped you fix your problem.
Thanks for reading.
❤️ You might like:
- How to fix "TypeError: ‘float’ object is not callable" in Python
- TypeError: 'int' object is not callable in Python (Fixed)
- TypeError: can only concatenate str (not “float”) to str (solutions)
- TypeError: can only concatenate str (not “int”) to str (Solutions)
- TypeError: can only concatenate str (not "bool") to str (Fixed)
Top comments (0)