π§ Error Fixes
Β· 1 min read
Python TypeError: Takes X Positional Arguments but Y Were Given β How to Fix It
TypeError: function() takes 2 positional arguments but 3 were given
Youβre passing more (or fewer) arguments to a function than it expects.
Fix 1: Wrong number of arguments
def greet(name):
print(f"Hello, {name}")
# β Too many arguments
greet("Alice", "Bob") # TypeError!
# β
Correct
greet("Alice")
Fix 2: Forgot self in a class method
class User:
# β Missing self β Python passes it automatically
def greet(name):
print(f"Hello, {name}")
user = User()
user.greet() # TypeError: takes 1 but 2 were given
# β
Add self
class User:
def greet(self, name):
print(f"Hello, {name}")
When you call user.greet(), Python automatically passes user as the first argument (self).
Fix 3: Passing a list instead of unpacking
def add(a, b):
return a + b
numbers = [3, 5]
# β Passing one list argument
add(numbers) # TypeError!
# β
Unpack with *
add(*numbers) # 8
Related resources
Related: Pip Install Error Fix