🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

Keyword Arguments (**kwargs)

AI & DATA SCIENCE // keyword-arguments-kwargs

**kwargs collects any number of extra keyword arguments passed to a function into a dictionary, letting the function accept arbitrary named inputs.

Syntax

def func(**kwargs):
    for key, value in kwargs.items():
        ...

func(a=1, b=2)

Deep Dive Course

A parameter prefixed with a double asterisk gathers every keyword argument the caller passes that doesn't match a named parameter, collecting them into a dict keyed by argument name — again, kwargs is just a conventional name. This is how functions can accept flexible, optional configuration without listing every possible option explicitly, and it's often paired with *args to build fully generic wrapper functions and decorators. At a call site, the double asterisk unpacks an existing dict into separate keyword arguments.

1Understanding Keyword Arguments (**kwargs)

A parameter prefixed with a double asterisk gathers every keyword argument the caller passes that doesn't match a named parameter, collecting them into a dict keyed by argument name — again, kwargs is just a conventional name. This is how functions can accept flexible, optional configuration without listing every possible option explicitly, and it's often paired with *args to build fully generic wrapper functions and decorators. At a call site, the double asterisk unpacks an existing dict into separate keyword arguments.

💡

Use a double asterisk at a call site to spread an existing dictionary out into keyword arguments — the mirror image of collecting them with **kwargs in a function definition, and a common pattern for forwarding configuration through a chain of function calls.

editor.html
def describe(**kwargs):
    for key, value in kwargs.items():
        print(f"{key}: {value}")

describe(name="Alice", age=30)
localhost:3000

2Practical Example

Here is a real-world application of Keyword Arguments (kwargs)** showing how it is used in production Python code.

editor.html
def build_url(base, **params):
    query = "&".join(f"{k}={v}" for k, v in params.items())
    return f"{base}?{query}"

print(build_url("/search", q="python", page=2))
localhost:3000

3Best Practices

Follow these guidelines when working with Keyword Arguments (kwargs)**:

1. Use **kwargs when a function needs to accept flexible, optional named configuration it doesn't fully know in advance

2. Prefer explicit named parameters with defaults over **kwargs when the accepted options are actually fixed and known

3. Combine *args and **kwargs in wrapper functions/decorators to forward arbitrary arguments through to the wrapped function unchanged

⚠️

Tip: Use a double asterisk at a call site to spread an existing dictionary out into keyword arguments — the mirror image of collecting them with **kwargs in a function definition, and a common pattern for forwarding configuration through a chain of function calls.

editor.html
def describe(**kwargs):
    for key, value in kwargs.items():
        print(f"{key}: {value}")

describe(name="Alice", age=30)
localhost:3000

Examples

Example 01Basic Usage
def describe(**kwargs):
    for key, value in kwargs.items():
        print(f"{key}: {value}")

describe(name="Alice", age=30)
Example 02Advanced Example
def build_url(base, **params):
    query = "&".join(f"{k}={v}" for k, v in params.items())
    return f"{base}?{query}"

print(build_url("/search", q="python", page=2))

Best Practices

  • Use **kwargs when a function needs to accept flexible, optional named configuration it doesn't fully know in advance
  • Prefer explicit named parameters with defaults over **kwargs when the accepted options are actually fixed and known
  • Combine *args and **kwargs in wrapper functions/decorators to forward arbitrary arguments through to the wrapped function unchanged

Interview Question

How would you write a generic decorator that works on any function, regardless of what arguments it takes?

Hint: Think about combining *args and **kwargs.

Define the wrapper function to accept *args and **kwargs itself, then forward both straight through to the wrapped function's call, unpacking them back into a normal call. Because *args captures any positional arguments and **kwargs captures any keyword arguments, the wrapper works regardless of the wrapped function's actual signature, which is exactly why this pattern is standard for writing general-purpose decorators.

Exercises

MediumPractice using Keyword Arguments (**kwargs) in a real scenario.
View Solution
def describe(**kwargs):
    for key, value in kwargs.items():
        print(f"{key}: {value}")

describe(name="Alice", age=30)

Frequently Asked Questions

How would you write a generic decorator that works on any function, regardless of what arguments it takes?

Define the wrapper function to accept *args and **kwargs itself, then forward both straight through to the wrapped function's call, unpacking them back into a normal call. Because *args captures any positional arguments and **kwargs captures any keyword arguments, the wrapper works regardless of the wrapped function's actual signature, which is exactly why this pattern is standard for writing general-purpose decorators.

Related Functions

arguments-argsdecoratorsdictionaries