Python TypeError: do_search() takes 1 positional argument but 2 were given - Solution and Explanation
This error, 'TypeError: do_search() takes 1 positional argument but 2 were given', means you're trying to call a function named 'do_search' with two arguments, but the function is only defined to accept one argument.
Let's illustrate this with an example:
def do_search(query):
# some code here
query = 'apple'
results = do_search(query, limit=10)
In this code snippet, the function 'do_search' expects only one argument, 'query'. However, you're calling it with two arguments: 'query' and 'limit=10'. This discrepancy causes the error.
Resolving the Error
To resolve this, you can follow these approaches:
- Remove the extra argument: If you don't need the 'limit' argument, simply remove it from the function call:
results = do_search(query)
- Modify the function definition: If you need the 'limit' argument, modify the 'do_search' function to accept it:
def do_search(query, limit):
# some code here
query = 'apple'
results = do_search(query, limit=10)
By making these adjustments, you ensure that the function receives the correct number of arguments and avoids the 'TypeError'. Remember to always check the function definition and the arguments you're passing to it to prevent these kinds of errors.
原文地址: https://www.cveoy.top/t/topic/ntBQ 著作权归作者所有。请勿转载和采集!