1 | initial version |
You can use '' and '*' before the function parameter to accept a variable number of positional and keyword arguments, respectively. Then, within the function, you can check if only known keywords are used.
Here's an example implementation:
def my_func(*args, **kwargs):
valid_keywords = ['foo', 'bar', 'baz']
for kwarg in kwargs:
if kwarg not in valid_keywords:
raise TypeError(f"Invalid keyword argument: '{kwarg}'")
# rest of the function code
Here, the my_func
function accepts any number of positional arguments and keyword arguments. It then checks if any of the keyword arguments are not in the valid_keywords
list, and raises a TypeError
if an unknown keyword is used. You can adjust the valid_keywords
list to include any keywords that are allowed.