*args and **kwargs
Accept any number of positional (*args) or keyword (**kwargs) arguments.
Easy
def log(*args, **kwargs):
print(args) # tuple of positional arguments
print(kwargs) # dict of keyword arguments
log(1, 2, level="warn") # (1, 2) {'level': 'warn'}
The * and ** also unpack when calling: f(*values, **options). This is common in wrapper functions and decorators that pass everything through.
Spotted a mistake or want to add detail? Suggest an improvement — Content Contributors' edits are reviewed by editors before they go live.
Discussion (0)
Sign in or join free to ask questions and discuss this topic.