Last updated: Apr 8, 2024
Reading timeยท8 min
The Python "TypeError: 'list' object is not callable" occurs when we try to
call a list as a function using parentheses ()
.
To solve the error, use square brackets when accessing a list at a specific
index, e.g. my_list[0]
.
Here is one example of how the error occurs.
my_list = ['bobby', 'hadz', 'com'] # โ๏ธ TypeError: 'list' object is not callable print(my_list(0)) # ๐๏ธ Uses parentheses instead of square brackets
The issue is that we used parenthesis instead of square brackets when accessing the list at a specific index.
Instead, use square brackets when accessing the list at an index.
my_list = ['bobby', 'hadz', 'com'] print(my_list[0]) # ๐๏ธ "bobby" print(my_list[1]) # ๐๏ธ "hadz"
Python indexes are zero-based, so the first item in a list has an index of 0
,
and the last item has an index of -1
or len(a_list) - 1
.
You should also use square brackets when getting a slice of a list.
my_list = ['bobby', 'hadz', 'com'] print(my_list[0:2]) # ๐๏ธ ['bobby', 'hadz'] print(my_list[1:3]) # ๐๏ธ ['hadz', 'com']
The syntax for list slicing is
a_list[start:stop:step]
.
The start
index is inclusive and the stop
index is exclusive (up to, but not
including).
If the start
index is omitted, it is considered to be 0
, if the stop
index
is omitted, the slice goes to the end of the list.
The error occurs for multiple reasons:
()
instead of square brackets []
.list()
functionAnother thing to look out for is that you're not overriding the built-in list() function.
# ๐๏ธ Overriding a built-in function list = ['bobby', 'hadz', 'com'] # โ๏ธ TypeError: 'list' object is not callable print(list([1, 2, 3]))
To solve the error in this situation, rename your variable and restart your script.
# โ Not overriding the built-in list anymore my_list = ['bobby', 'hadz', 'com'] print(list([1, 2, 3]))
The two variables now have different names, so the issue is resolved.
Another common cause of the error is simply trying to call a list as a function.
my_list = ['a', 'b', 'c'] # โ๏ธ TypeError: 'list' object is not callable print(my_list())
To solve the error, you either have to remove the parentheses or figure out how the variable got assigned a list instead of a function or a class.
my_list = ['bobby', 'hadz', 'com'] print(my_list) # ๐๏ธ ['bobby', 'hadz', 'com']
We removed the parentheses to print the value of the variable.
The error is also caused when we have a class method and a class property with the same name.
class Employee(): def __init__(self, tasks): # ๐๏ธ this attribute hides the method self.tasks = tasks # ๐๏ธ same name as class variable def tasks(self): return self.tasks emp = Employee(['dev', 'test']) # โ๏ธ TypeError: 'list' object is not callable print(emp.tasks())
The Employee
class has a method and an attribute with the same name.
To solve the error, you have to rename the class method.
class Employee(): def __init__(self, tasks): self.tasks = tasks def get_tasks(self): return self.tasks emp = Employee(['dev', 'test']) print(emp.get_tasks()) # ๐๏ธ ['dev', 'test']
Once you rename the method, you will be able to call it without any issues.
Make sure you don't have a function and a variable with the same name.
def example(): return 'bobbyhadz.com' # ๐๏ธ Shadowing the function example = ['a', 'b', 'c'] # โ๏ธ TypeError: 'list' object is not callable print(example())
The example
variable shadows the function with the same name, so when we try
to call the function, we actually end up calling the variable.
Renaming the variable or the function solves the error.
def example(): return 'bobbyhadz.com' my_list = ['a', 'b', 'c'] print(example()) # ๐๏ธ bobbyhadz.com print(my_list) # ๐๏ธ ['a', 'b', 'c']
The variable and the function are now named differently, so the issue is resolved.
Here is another example of how the error occurs.
def example(): return ['bobby', 'hadz', 'com'] # โ๏ธ TypeError: 'list' object is not callable example()()
Notice that we used two sets of parentheses.
The first set invoked the example
function. The function returns a list, so
the second set of parentheses invoked the list
object.
To solve the error, remove the second set of parentheses.
def example(): return ['bobby', 'hadz', 'com'] print(example()) # ๐๏ธ ['bobby', 'hadz', 'com']
Now we only call the function once and print the result.
To solve the "TypeError: 'list' object is not callable" error, make sure:
()
instead of square
brackets []
.The Python "TypeError: 'set' object is not callable" occurs when we try to
call a set object as a function, e.g. by overriding the built-in set()
function.
To solve the error, make sure you aren't overriding set()
and resolve any
clashes between function and variable names.
Here is one example of how the error occurs.
# ๐๏ธ Overriding the built-in set() set = set(['a', 'b', 'c']) # โ๏ธ TypeError: 'set' object is not callable set(['d', 'e', 'f'])
We override the built-in
set() function, setting
it to a set
object and try to call it as a function.
You can solve the error by renaming your variable.
# โ Not overriding the built-in set() anymore my_set = set(['a', 'b', 'c']) print(set(['d', 'e', 'f'])) # ๐๏ธ {'d', 'f', 'e'}
We gave the variable a different name, so it no longer clashes with the built-in
set()
function.
set
as a functionAnother common cause of the error is simply trying to call a set
as a
function.
my_set = {'a', 'b', 'c'} # โ๏ธ TypeError: 'set' object is not callable my_set()
To solve the error, either remove the parentheses or figure out how the variable
got assigned a set
instead of a function or a class.
my_set = {'a', 'b', 'c'} print(my_set) # ๐๏ธ {'a', 'c', 'b'}
Removing the parentheses resolved the issue and we were able to print the set
.
If the variable is supposed to point to a function or method, track down where
it got assigned a set
and correct the assignment.
Make sure you don't have a function and a variable with the same name.
def example(): return 'bobbyhadz.com' example = {'a', 'b', 'c'} # โ๏ธ TypeError: 'set' object is not callable print(example())
example
variable shadows the function with the same name, so when we try to call the function, we actually end up calling the variable.Renaming the variable or the function solves the error.
def example(): return 'bobbyhadz.com' a_set = {'a', 'b', 'c'} print(example()) # ๐๏ธ bobbyhadz.com
We gave the variable a different name, so it no longer clashes with the function.
Now we are able to call the function without any issues.
The error is also caused when we have a class method and a class property with the same name.
class Employee(): def __init__(self, tasks): # ๐๏ธ this attribute hides the method self.tasks = tasks # ๐๏ธ same name as class variable def tasks(self): return self.tasks emp = Employee({'dev', 'test'}) # โ๏ธ TypeError: 'set' object is not callable print(emp.tasks())
The Employee
class has a method and an attribute with the same name.
The attribute hides the method, so when we try to call the method on an instance of the class, we get the 'set' is not callable error.
To solve the error, you have to rename the class method.
class Employee(): def __init__(self, tasks): self.tasks = tasks def get_tasks(self): return self.tasks emp = Employee({'dev', 'test'}) print(emp.get_tasks()) # ๐๏ธ {'test', 'dev'}
We renamed the method, so it no longer clashes with the class property.
Now, we can call the method on an instance of the class without any issues.
The Python "TypeError: 'generator' object is not callable" occurs when we try to call a generator object as if it were a function.
To solve the error, remove the parentheses and make sure you don't have any clashes between function and variable names.
Here is an example of how the error occurs
def g(): yield 1 yield 2 yield 3 gen = g() print(type(gen)) # ๐๏ธ <class 'generator'> # โ๏ธ TypeError: 'generator' object is not callable print(gen())
We tried to call a generator object as a function which caused the error.
To solve the error, remove the parentheses.
def g(): yield 1 yield 2 yield 3 gen = g() # ๐๏ธ creates generator for i in gen: print(i) # ๐๏ธ 1, 2, 3
We use parentheses when creating a generator. However, you don't have to use parentheses when iterating over one in a for loop.
Make sure you don't have a second set of parentheses because the first set creates a generator object and the second set calls the generator object, causing the error.
next()
function to get the next itemAlternatively, you can pass the generator
to the next()
function to retrieve
the next item.
def g(): yield 1 yield 2 yield 3 gen = g() print(next(gen)) # ๐๏ธ 1 print(next(gen)) # ๐๏ธ 2 print(next(gen)) # ๐๏ธ 3
Make sure you don't have an extra set of parentheses when creating the generator.
def g(): yield 1 yield 2 yield 3 # โ๏ธ TypeError: 'generator' object is not callable gen = g()() # ๐๏ธ Remove the extra set of parentheses
Another common cause of the error is having a clash between variable and function names.
def g(): yield 1 yield 2 yield 3 def gen(): return 'bobbyhadz.com' gen = g() # โ๏ธ TypeError: 'generator' object is not callable gen()
The gen
variable shadows the gen()
function, so when we try to call the
function, we end up calling the variable.
To solve the error, rename the variable or the function.
def g(): yield 1 yield 2 yield 3 def greet(): return 'bobbyhadz.com' gen = g() print(greet()) # ๐๏ธ "bobbyhadz.com"