TypeError: takes 0 positional arguments but 1 was given

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
4 min

banner

# Table of Contents

  1. TypeError: takes 0 positional arguments but 1 was given
  2. TypeError: takes 0 positional arguments but 2 were given

# 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

# Forgetting to specify the self argument in a class method

Here is an example of how the error occurs.

main.py
class Employee(): # ๐Ÿ‘‡๏ธ Forgot to take the self arg def get_name(): return 'Bobby Hadz' emp = Employee() # โ›”๏ธ TypeError: Employee.get_name() takes 0 positional arguments but 1 was given print(emp.get_name())

forgetting to specify self argument in class method

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 the self arg def get_name(self): return 'Bobby Hadz' # โœ… Create an instance of the class emp = Employee() print(emp.get_name()) # ๐Ÿ‘‰๏ธ "Bobby Hadz"

self is automatically passed to class method

self represents an instance of the class - the instance on which the method was called.

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

If your method takes self as its first argument, make sure to call the method on an instance of the class and not on the class itself.

# Use a static method if your method doesn't use the self argument

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 'Bobby Hadz' emp = Employee() print(emp.get_name()) # ๐Ÿ‘‰๏ธ "Bobby Hadz"

A static method doesn't receive an implicit first argument and can be called on the class or on an instance of the class.

# Forgetting to specify an argument in a function's definition

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']

# Overriding built-in methods and classes

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 'bobbyhadz.com' # โ›”๏ธ 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.

# TypeError: takes 0 positional arguments but 2 were given

The Python "TypeError: takes 0 positional arguments but 2 were given" occurs for multiple reasons:

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

typeerror takes 0 positional arguments but 2 were given

# Forgetting to specify the parameters of a function

Here is an example of how the error occurs.

main.py
def do_math(): return a + b # โ›”๏ธ TypeError: do_math() takes 0 positional arguments but 2 were given result = do_math(10, 15)

We forgot to specify the parameters the do_math function takes and called the function passing it 2 arguments which caused the error.

To solve the error, make sure to specify the parameters the function takes in its definition.

main.py
def do_math(a, b): return a + b result = do_math(10, 15) print(result) # ๐Ÿ‘‰๏ธ 25

Now the function takes 2 arguments - a and b and gets passed 2 arguments, so the error is resolved.

If your function doesn't need to take any arguments but gets called with 2, you have to remove the arguments from the function call.

main.py
def do_math(): return 10 + 10 result = do_math() print(result) # ๐Ÿ‘‰๏ธ 20

Now the function doesn't take any arguments and doesn't get passed any.

# Specify self as the first argument in classes

If you get the error when working with classes, make sure to specify self as the first argument in your class methods.

main.py
class Employee(): # ๐Ÿ‘‡๏ธ Specify self as the first arg def get_name(self, name): return name emp = Employee() print(emp.get_name('Bobby Hadz')) # ๐Ÿ‘‰๏ธ "Bobby Hadz"
Python automatically passes self to the class method when it is called.

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

# Use a static method if you don't use the self keyword in a method

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(name): return name emp = Employee() print(emp.get_name('Bobby Hadz')) # ๐Ÿ‘‰๏ธ "Bobby Hadz"

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

Make sure you aren't overriding any built-in functions or classes by declaring a function with the same name as that can also cause the error.

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.

Copyright ยฉ 2024 Borislav Hadzhiev