Table of Contents
ToggleWhat is the “Python Got Multiple Values for Argument” Error mean?
In simple terms, the “multiple values for argument” error occurs when we pass more than one value for arguments of the function or method and Python doesn’t know which one to use.
When we define a function or method we specify the arguments or values. If we unintentionally provide conflicting values through keyword argument or positional argument. The compiler raises a error.
Let’s first clear the terminology of keyword arguments and positional argument. I will be using sum_two_number function to explain the above.
def sum_two_number(number_1,number_2): return sum number_1+number_2
Positional Argument are values passed to function based on position of parameter in the function call.If you want to add number_1 and number_2 which have values 2, 4 by passing positional values to function sum_two_number. We would call it
sum_two_number(2,4).
In Keyword argument approach we pass value by assigning value to parameter using following syntax ( Parameter=value ). For example if we want to pass values to above function sum_two_number with number_1 value as 10 and number_2 value as 20.
sum_two_number(number_1=10,number_2=20)
Now, let’s explore various cases of “TypeError got multiple values for argument” in python
If you want more information about keyword and positional argument click here.
Case 1 : Function got multiple values for argument
def my_function(a, b): Return a+bCorrect way to call the function is
my_function(a=2,b=5) my_function(2,5) my_function(2,b=5) my_function(a=2,5)
Which would give output as 7. But when we sometime unintentionally we call function with both positional value and keyword value for the same parameter like b in the below function.
my_function(10, 30, b=20)
Output
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) in () 2 print(a, b) 3 ----> 4 my_function(10, 30, b=20) TypeError: my_function() got multiple values for argument Solution: Call the function either with positional argument or keyword arguments.and see to that for each argument in the function only a single value is passed like shown below
Solution: Call the function either with positional argument or keyword arguments.and see to that for each argument in the function only a single value is passed like shown below
my_function(a=2,b=5) my_function(2,5) my_function(2,b=5) my_function(a=2,5)
Case 2 : TypeError: __init__() got multiple values for argument
The error cames when we use both *args (poistional argument) and **kwargs (keyword argument) further more accidentally pass 2 values for same parameter of method or function of class. For example in the below code
class MyClass: def __init__(self, a, b): self.a = a self.b = b obj = MyClass(1, 2, b=3)
We are using both poistional argument and keyword arguments to pass value in __init__(self,a,b) method.for parameter b. Which causes the error “__init__() got multiple values for argument”
Solution: Map each value to argument and see to that each argument gets single value as shown below
obj = MyClass(a=2,b=5) obj = MyClass(2,5) obj = MyClass(2,b=5) obj = MyClass(a=2,5)
Case 3 : Python requests.post() got multiple values for argument data
import requests data ={ "name": "morpheus", "job": "leader" } url = https://reqres.in//api/users response = requests.post(url, data, data=data)Output:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last)Solution : Pass the data value as either positional argument or keyword argument as shown below. response = requests.post(url, data=data)in () 6 7 url = "https://reqres.in//api/users" ----> 8 response = requests.post(url, data, data=data) TypeError: post() got multiple values for argument 'data' |
Case 4: Python webdriver __init__ got multiple values for argument options
If we are using the code “webdriver.Chrome(r’/usr/bin/chromedriver’, options=chrome_options)” for latest version of selenium for opening a web page, we will get “webdriver __init__ got multiple values for argument ”
Full python code
from selenium import webdriver from selenium.webdriver.chrome.options import Options # Import the Options class to configure Chrome settings # Define ChromeOptions to customize the browser's behavior chrome_options = Options() # Adding command-line arguments to the ChromeOptions object chrome_options.add_argument('--headless') # Run Chrome in headless mode (without a graphical interface) chrome_options.add_argument('--no-sandbox') # Disable sandboxing (important in certain environments like Docker) chrome_options.add_argument('--disable-dev-shm-usage') # Disable shared memory usage (helps in environments with limited memory) # Step 2: Initialize the WebDriver and pass the path to the ChromeDriver and the options # The executable path for ChromeDriver is passed directly to webdriver.Chrome, along with the options for the browser browser = webdriver.Chrome(r'/usr/bin/chromedriver', options=chrome_options) # Step 3: Perform actions with the browser # opening a website browser.get('https://www.example.com') # Step 4: Close the browser after use browser.quit()
The error is coming due to recent change in selenium library where now first argument is no longer executable_path, but options. While Initializing we are passing two values for same argument “options” in the above code which causes the error. Selenium now requires passing a Service object to specify the path to the ChromeDriver binary as second parameter and options as first parameter.
Correct code:
selenium import webdriver from selenium.webdriver.chrome.service import Service # Define the Service with the path to the ChromeDriver service = Service(executable_path=r'/usr/bin/chromedriver') # Define the ChromeOptions (you can add custom arguments here) options = webdriver.ChromeOptions() options.add_argument('--headless') # Run in headless mode (no UI) options.add_argument('--no-sandbox') # Disable sandbox (for certain environments) options.add_argument('--disable-dev-shm-usage') # Use a shared memory space # Create the WebDriver instance with the Service and options driver = webdriver.Chrome(service=service, options=options) # Perform actions with the driver # For example, you could open a website driver.get('https://www.example.com') # Once done, quit the driver to clean up driver.quit()
Tips to avoid got multiple values for argument
- Either pass positional or keyword arguments for eacgh function call and follow the same through out the code.
- Constantly do code reviews and check function parameters and values are passed correctly.
- Performing unit testing. Unit testing checks for common errors in argument passing. This can help identify issues early in the development process.
- Always double check function signatures so that we are confident of passing the correct number of arguments and types expected by the function.
Conclusion
In the blog, we understood the cause of “got multiple values for argument” error. The solution to error was simple, check for argument of a function who got two values while calling it. After identification, just remove the duplicated value.
You can also learn how to export a pandas dataframe to csv without index in python.
Pingback: How to Import Python Files from the Same Directory -