There is a problem with *args and **kwargs regarding the task.
Skaperen Wrote:to accept zero or one argument
There is no limit for that in *args and **kwargs
Just add:
>>> @limit_args(1)
... def func(*args):
... if not args:
... args = 'default'
... return args
>>> func()
'default'
>>> func('hello')
('hello',)
>>> func('hello', 'world')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 5, in new_f
TooManyArguments: 1 args accepted at most, 2 args passed
1 args accepted at most, 2 args passed
>>> @limit_args(1)
... def something(**kwargs):
... if kwargs:
... for key, value in kwargs.items():
... print(f'{key} is {value}')
... else:
... print('nothing to see here')
>>> something(place="London")
place is London
>>> something(place="London", wetness="raining heavily")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 5, in new_f
TooManyArguments: 1 args accepted at most, 0 args passed
1 args accepted at most, 0 args passed
The missing code for that.
class TooManyArguments(Exception):
pass
def limit_args(n):
def limit_decorator(func):
def new_f(*args, **kwargs):
if len(args or kwarg) > n:
raise TooManyArguments(f"{n} args accepted at most, {len(args)} args passed")
return func(*args, **kwargs)
return new_f
return limit_decorator
But this going a little overboard,as the simple solution shown solve that task.