Last updated: Apr 8, 2024
Reading timeยท4 min
The Python "TypeError: takes 0 positional arguments but 1 was given" occurs for multiple reasons:
self
argument in a class method.self
argument in a class methodHere is an example of how the error occurs.
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())
We forgot to specify the self
argument in the definition of the get_name
class method.
self
to the class method when it is called, so a method that takes no arguments gets passed one which causes the error.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 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.
self
argumentIf your method doesn't make use of the self
argument, you can declare a
static method.
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.
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.
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.
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.
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.
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.
The Python "TypeError: takes 0 positional arguments but 2 were given" occurs for multiple reasons:
self
arg in a class method.Here is an example of how the error occurs.
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.
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.
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.
self
as the first argument in classesIf you get the error when working with classes, make sure to specify self
as
the first argument in your class methods.
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"
self
to the class method when it is called.self
represents an instance of the class, so when we assign a variable as
self
keyword in a methodIf your method doesn't make use of the self
argument, you can declare a
static method.
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.