TypeError: takes 0 positional arguments but 1 was given

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

TypeError: takes 0 positional arguments but 1 was given #

The Python "TypeError: takes 0 positional arguments but 1 was given" occurs for multiple reasons:

  1. Forgetting to specify the self argument in a class method.
  2. Forgetting to specify an argument in a function.
  3. Passing an argument to a function that doesn't take any arguments.
  4. Overriding a built-in function by mistake.

typeerror takes 0 positional arguments but 1 was given

Here is an example of how the error occurs.

main.py
class Employee(): # 👇️ forgot to take self arg def get_name(): return 'Alice' emp = Employee() # ⛔️ TypeError: Employee.get_name() takes 0 positional arguments but 1 was given print(emp.get_name())

We forgot to specify the self argument in the definition of the get_name class method.

Python automatically passes self to the class method when it is called, so a method that takes no arguments gets passed one which causes the error.
main.py
class Employee(): # 👇️ specify self arg def get_name(self): return 'Alice' emp = Employee() print(emp.get_name()) # 👉️ "Alice"

self represents an instance of the class, so when we assign a variable as

Specifying the self argument in the method's definition solves the error.

If your method doesn't make use of the self argument, you can declare a static method.

main.py
class Employee(): @staticmethod def get_name(): return 'Alice' emp = Employee() print(emp.get_name()) # 👉️ "Alice"

A static method does not receive an implicit first argument and can be called on the class or on an instance of the class.

The error is also caused when you forget to specify an argument in a function's definition or pass an argument to a function that doesn't take any.
main.py
def get_list(): return ['a', 'b', 'c'] # ⛔️ TypeError: get_list() takes 0 positional arguments but 1 was given result = get_list('a')

The get_list function doesn't take any arguments but it gets passed 1 when it is invoked.

In this situation, we either have to update the function's declaration and take the argument or remove the argument from the function invocation.

Here is an example of removing the argument from the function invocation.

main.py
def get_list(): return ['a', 'b', 'c'] result = get_list() print(result) # 👉️ ['a', 'b', 'c']

And here is an example of specifying the argument in the function's definition.

main.py
def get_list(x): return ['a', 'b', 'c', x] result = get_list('d') print(result) # 👉️ ['a', 'b', 'c', 'd']

Another common cause of the error is overriding built-in functions and classes by declaring a function with the same name.

main.py
def str(): return 'hello' # ⛔️ TypeError: str() takes 0 positional arguments but 1 was given result = str(123)

We declared an str function that shadows the built-in str class.

When we call str(), we are actually calling our own function and not the built-in class.

Make sure you don't have functions that share the same name with built-ins.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.