How to Resolve Python Error "TypeError: missing X required positional argument(s)"
The TypeError: ... missing X required positional argument(s): 'arg_name'
(where X is 1, 2, or more) is a common error in Python. It occurs when you call a function or instantiate a class without providing values for all the required parameters defined in the function or the class's __init__
method. Python needs these arguments to execute the code correctly.
This guide explains why this error happens and provides standard solutions like supplying the arguments or setting default parameter values.
Understanding the Error: Required Arguments
When you define a function or a class's __init__
method in Python, you specify the parameters it accepts. Parameters that do not have a default value assigned in the definition are considered required positional arguments. When you call that function or create an instance of the class, you must provide a value (an argument) for each of these required parameters, in the correct order.
The TypeError: missing X required positional argument(s): 'arg_name(s)'
tells you exactly how many arguments Python expected but didn't receive, and it often lists the names of the missing ones.
Scenario 1: Missing Arguments in Function Calls
This happens when calling a regular function defined with def
.
Cause: Calling Function Without Providing All Arguments
# Function definition expects two arguments: message and count
def repeat_message(message, count):
for _ in range(count):
print(message)
try:
# Error Scenario: Calling with only one argument (missing 'count')
# ⛔️ TypeError: repeat_message() missing 1 required positional argument: 'count'
repeat_message("Hello")
except TypeError as e:
print(e)
try:
# Error Scenario: Calling with no arguments (missing 'message' and 'count')
# ⛔️ TypeError: repeat_message() missing 2 required positional arguments: 'message' and 'count'
repeat_message()
except TypeError as e:
print(e)
Output:
repeat_message() missing 1 required positional argument: 'count'
repeat_message() missing 2 required positional arguments: 'message' and 'count'
The function repeat_message
requires both message
and count
, but the calls provided fewer than two arguments.
Solution 1: Provide Required Arguments in the Call
Supply a value for each required parameter when you call the function.
def repeat_message(message, count):
for _ in range(count):
print(message)
# ✅ Corrected Call: Provide both arguments
print("Calling repeat_message('Hi', 3):")
repeat_message("Hi", 3)
Output:
Calling repeat_message('Hi', 3):
Hi
Hi
Hi
Solution 2: Set Default Parameter Values in Function Definition
If some parameters should have a standard value if none is provided by the caller, assign a default value in the function definition using =
. Parameters with default values become optional.
# ✅ Set default values for parameters
def repeat_message_defaults(message="Default Message", count=1):
for _ in range(count):
print(message)
print("Calling with defaults:")
repeat_message_defaults() # Uses both defaults
print("Calling with message only:")
repeat_message_defaults("Specific Message") # Uses default for count
print("Calling with count only:")
repeat_message_defaults(count=2) # Uses default for message, specifies count by name
print("Calling with both:")
repeat_message_defaults("Another", 3) # Overrides both defaults
Output:
Calling with defaults:
Default Message
Calling with message only:
Specific Message
Calling with count only:
Default Message
Default Message
Calling with both:
Another
Another
Another
Now the function can be called without providing arguments for parameters that have defaults.
Caution: Default Mutable Arguments (Lists, Dicts)
Avoid using mutable objects (like lists []
or dictionaries {}
) as default parameter values directly. Default arguments are evaluated only once when the function is defined. If a mutable default is modified inside the function, that modification persists across subsequent calls, which is usually unintended.
Problematic Example:
def add_to_list(item, target_list=[]): # ⚠️ Mutable default []
target_list.append(item)
print(f"Inside function: {target_list}")
return target_list
list1 = add_to_list(1) # Appends 1 to the *single* default list object
list2 = add_to_list(2) # Appends 2 to the *same* default list object!
print(f"List 1 result: {list1}") # Output: [1, 2]
print(f"List 2 result: {list2}") # Output: [1, 2] (Unexpected!)
Correct Pattern:
def add_to_list_safe(item, target_list=None): # Default to None
if target_list is None:
target_list = [] # Create a *new* list inside the call if needed
target_list.append(item)
print(f"Inside safe function: {target_list}")
return target_list
list3 = add_to_list_safe(1)
list4 = add_to_list_safe(2)
print(f"List 3 safe result: {list3}") # Output: [1]
print(f"List 4 safe result: {list4}") # Output: [2] (Correct!)
Use None
as the default and create a new mutable object inside the function body if necessary.
Scenario 2: Missing Arguments When Instantiating Classes (__init__
)
This occurs when creating an object (an instance) of a class that has an __init__
method requiring arguments.
Cause: Instantiating Class Without Providing __init__
Arguments
The __init__
method is the constructor. When you call ClassName(arg1, arg2)
, these arguments are passed to __init__(self, arg1, arg2)
. If __init__
defines required parameters (other than self
), you must provide corresponding arguments when creating the instance.
class Product:
# __init__ requires 'name' and 'price'
def __init__(self, name, price):
self.name = name
self.price = price
try:
# Error Scenario: Missing 'name' and 'price'
# ⛔️ TypeError: Product.__init__() missing 2 required positional arguments: 'name' and 'price'
item = Product()
except TypeError as e:
print(e)
try:
# Error Scenario: Missing 'price'
# ⛔️ TypeError: Product.__init__() missing 1 required positional argument: 'price'
item = Product("Widget")
except TypeError as e:
print(e)
Solution 1: Provide Required Arguments During Instantiation
Pass values for all required __init__
parameters when creating the object.
class Product:
def __init__(self, name, price):
self.name = name
self.price = price
# ✅ Correct Instantiation: Provide both arguments
item1 = Product("Gadget", 19.99)
print(f"Created: {item1.name} - ${item1.price}")
# Output: Created: Gadget - $19.99
Solution 2: Set Default Parameter Values in __init__
Definition
Make __init__
parameters optional by providing default values in the method definition.
class ProductWithDefaults:
# ✅ Set default values for parameters
def __init__(self, name="Default Product", price=0.0):
self.name = name
self.price = price
# Can now instantiate without arguments
item_default = ProductWithDefaults()
print(f"Default item: {item_default.name} - ${item_default.price}")
# Output: Default item: Default Product - $0.0
# Or provide specific values
item_specific = ProductWithDefaults(name="Gizmo", price=9.95)
print(f"Specific item: {item_specific.name} - ${item_specific.price}")
# Output: Specific item: Gizmo - $9.95
Remember the caution about mutable defaults (Section 2.4) applies equally to __init__
methods.
Solution 3: Remove Parameter if Value is Hardcoded
If an __init__
parameter was only used to immediately assign a fixed, hardcoded value to an instance attribute, you might not need the parameter at all.
Code Before (with unnecessary parameter):
class Config:
def __init__(self, default_mode): # default_mode always set to 'auto'
self.mode = 'auto'
Code After (parameter removed):
class Config:
# ✅ Hardcode directly if parameter wasn't needed
def __init__(self):
self.mode = 'auto' # Fixed value
# Now instantiation doesn't require the argument
conf = Config()
print(f"Config mode: {conf.mode}") # Output: auto
Note on self
The first parameter of instance methods (including __init__
) is conventionally named self
. It represents the instance being created or acted upon. Python passes self
automatically; you do not provide an argument for it when calling the method or instantiating the class. The error message "missing 1 required positional argument: 'self'" indicates a different problem, usually calling an instance method as if it were a static or class method.
Note on Inheritance (super().__init__
)
If your class inherits from another class (a parent class) and the parent's __init__
requires arguments, you must provide those arguments when calling the parent's __init__
(typically via super().__init__(...)
). Forgetting to do so can cause this TypeError
originating from the parent's __init__
.
class Vehicle:
def __init__(self, num_wheels): # Requires num_wheels
self.num_wheels = num_wheels
class Car(Vehicle):
def __init__(self, color): # Takes color
self.color = color
# Forgetting to pass required arg to parent's __init__ would cause error:
# super().__init__() # ⛔️ TypeError: Vehicle.__init__() missing 1 required positional argument: 'num_wheels'
# ✅ Correct: Pass required argument(s) to parent's init
super().__init__(num_wheels=4)
my_car = Car("Red")
print(f"My car has {my_car.num_wheels} wheels and color {my_car.color}")
Debugging the Error
- Read the Error Message Carefully: It tells you the function/method name (
function()
orClassName.__init__()
), the number of missing arguments, and usually their names. - Check the Definition: Look at the
def function(...)
ordef __init__(...)
line for the function/method mentioned. Identify parameters without default values. - Check the Call/Instantiation: Look at the line where you called the function or instantiated the class (
ClassName(...)
). Ensure you are passing a value for every required parameter identified in step 2.
Conclusion
The TypeError: ... missing X required positional argument(s)
clearly indicates a mismatch between the parameters defined for a function or __init__
method and the arguments provided when calling/instantiating it.
The solutions are:
- Provide all required arguments when making the function call or creating the class instance.
- Modify the function or
__init__
definition to add default values (parameter=default
) to make arguments optional. - If appropriate, remove parameters from the definition if they are no longer needed or if their values can be hardcoded or determined internally.
Ensure you understand which parameters are required and provide corresponding arguments to resolve this common Python error. Be cautious with mutable default arguments.