Quick answer: kwargs.get reads an optional keyword argument without raising KeyError when the key is absent. Use it for genuinely optional options, distinguish a missing key from an explicit None with a sentinel or membership test, and prefer explicit parameters for stable APIs.

In Python, **kwargs collects extra keyword arguments into a dictionary. Inside the function, you can read those optional entries with normal dictionary methods such as get(), membership checks, pop(), and key validation.
kwargs.get("name", default) is useful when an argument is optional. It returns the supplied value when the key exists and returns the default when the key is missing. That makes it cleaner than repeated if checks for simple options.
The tradeoff is that **kwargs hides accepted options from the function signature. Use it when flexibility is useful, such as wrappers, compatibility layers, or APIs that forward options. For ordinary functions, explicit keyword-only parameters are often clearer.
The official Python keyword arguments tutorial covers keyword arguments and **name parameters, while the dict.get documentation explains dictionary lookup defaults.
Read A Simple kwargs Value
A function can accept named options that are not listed as formal parameters.
def build_report(**kwargs):
title = kwargs.get("title", "Untitled")
return f"Report: {title}"
print(build_report(title="Sales"))
print(build_report())
The first call supplies title. The second call uses the default because the key is missing.
This pattern is readable when the option is genuinely optional and has a sensible fallback. If the option is required, list it as a normal keyword parameter instead of retrieving it from kwargs.
Set Defaults For Optional Arguments
Use get() when a missing key should fall back to a known default.
def paginate(items, **kwargs):
limit = kwargs.get("limit", 10)
offset = kwargs.get("offset", 0)
return items[offset:offset + limit]
numbers = list(range(20))
print(paginate(numbers, limit=5))
Defaults should match the expected type. If limit should be an integer, use an integer default.
It is also worth validating values after reading them. A default can make a key optional, but it does not prove that a supplied value has the right type or range.
Distinguish Missing From None
Sometimes None is a meaningful value. Use a sentinel object when you need to know whether the caller omitted the key.
missing = object()
def connect(**kwargs):
timeout = kwargs.get("timeout", missing)
if timeout is missing:
return "timeout was not supplied"
return f"timeout: {timeout}"
print(connect())
print(connect(timeout=None))
This keeps “not supplied” separate from “supplied as None.” That distinction matters for configuration APIs.
Sentinels are useful when None means “disable this behavior” or “clear this setting.” Without a sentinel, missing and explicit None can look the same.

Remove Consumed Options With pop
pop() reads a key and removes it from the dictionary, which helps validate leftover options.
def make_client(**kwargs):
retries = kwargs.pop("retries", 3)
timeout = kwargs.pop("timeout", 30)
if kwargs:
raise TypeError(f"unknown options: {sorted(kwargs)}")
return {"retries": retries, "timeout": timeout}
print(make_client(retries=5))
This pattern prevents misspelled keyword arguments from being ignored silently.
Rejecting unknown keys is especially important in public APIs. A caller who writes timeot instead of timeout should get a clear error, not a default timeout that hides the typo.
Merge Defaults With kwargs
For several options, create a defaults dictionary and merge caller options into it.
def chart_options(**kwargs):
defaults = {
"width": 800,
"height": 400,
"theme": "light",
}
options = defaults | kwargs
return options
print(chart_options(theme="dark"))
The merge operator creates a new dictionary where caller-provided keys override defaults.
This style is compact for configuration objects. It also keeps the original defaults unchanged, which makes repeated calls safer and easier to reason about.

Pass kwargs To Another Function
Use **kwargs to forward accepted options to another function after validating them.
def render_text(text, *, uppercase=False, prefix=""):
if uppercase:
text = text.upper()
return f"{prefix}{text}"
def render_message(text, **kwargs):
allowed = {"uppercase", "prefix"}
extra = set(kwargs) - allowed
if extra:
raise TypeError(f"unknown options: {sorted(extra)}")
return render_text(text, **kwargs)
print(render_message("hello", uppercase=True, prefix="> "))
Forwarding is useful, but validate the accepted keys so errors stay close to the caller.
Without validation, a typo may travel through several wrapper functions before it fails. Keeping the check near the entry point makes debugging faster.
Practical Guidance
Use kwargs.get() for simple optional values. Use a sentinel when None has meaning. Use pop() when you want to consume known options and reject anything left over.
Do not use **kwargs as a way to hide every possible option. Explicit parameters are easier to read and document. Keep **kwargs for flexible APIs, wrappers, and compatibility layers where extra keyword arguments are expected.
When a function accepts several options, document the accepted keys, expected types, and defaults. That makes the function easier to call and reduces debugging time when a key is misspelled.
If the set of options stops changing, consider replacing **kwargs with explicit keyword-only parameters. The call site becomes easier to read, and tools can offer better autocomplete and type checks.
The short rule is practical: use get() for optional keys, use pop() when validating leftovers, and do not let unknown keyword arguments pass silently.
That keeps flexible functions convenient without making mistakes disappear during normal use.
It also makes future refactors much easier to review and test safely.
Read Optional Keys
kwargs is a dictionary of keyword arguments collected by a function. kwargs.get(‘timeout’) returns None when the key is missing unless a different default is supplied.

Distinguish Missing From None
A caller can pass key=None, which is different from not passing the key at all. Use key in kwargs or a unique sentinel default when those states have different meanings.
Validate Values After Reading
get only handles presence; it does not validate type, range, or allowed values. Parse and validate the result before passing it into a lower-level operation.

Require Keys Explicitly
Use kwargs[‘name’] when the contract requires a key and a missing value should fail fast, or raise a clear custom error after an explicit check. Silent defaults can hide configuration mistakes.
Prefer A Clear Public API
A long-lived function is easier to document and type-check with named parameters. Use kwargs for extensible options, forwarding, adapters, and APIs where optional names are intentionally open-ended.
Test Forwarding And Defaults
Test missing keys, explicit None, falsey values, invalid types, unknown options, default precedence, and forwarding to another function. Assert the final normalized configuration rather than only the dictionary lookup.
The official mapping documentation describes mapping behavior, while Python’s built-in dict provides get. Related Python Pool references include dictionaries and tests.
For related option handling, compare mapping access, default-value tests, and configuration diagnostics before forwarding kwargs.
Frequently Asked Questions
What does kwargs.get do?
It reads a key from the keyword-arguments dictionary and returns a default when the key is absent.
How is kwargs.get different from kwargs[key]?
get avoids KeyError for a missing key, while bracket access makes the key required and fails fast when it is absent.
What if the caller passes None?
get distinguishes a present key with value None from a missing key only when you inspect membership or use a sentinel default.
When should I avoid kwargs?
Prefer explicit named parameters when the function has a stable public API; use kwargs for genuinely optional, extensible, or forwarded keyword options.