ary()
Create a function that accepts up to n arguments, ignoring additional ones.
Implementation
Args: func: The function to cap arguments for n: The arity cap (if None, uses function's natural arity) Returns: A function that accepts at most n arguments
Example
def add_four(a, b, c, d):
return a + b + c + d
capped = ary(add_four, 2)
capped(1, 2, 3, 4) # Only uses first 2 args
Expected output: 3
Source Code
def ary(func: Callable, n: Optional[int] = None) -> Callable:
_validate_callable(func)
if n is None:
n = func.__code__.co_argcount
def wrapper(*args, **kwargs):
return func(*args[:n], **kwargs)
return _copy_function_metadata(wrapper, func)