More KWARGS!
Object destructuring of function parameters for Python!
Motivation
Javascript has object destructuring, and it can be used for function parameters. This has a couple of benefts over Python's keyword arguments:
- Extra caller parameters are ignored (eg
f({a, b, c})) - Duplicate parameters are handled elegantly (eg
f({a, a}))
The mo-kwargs library provides this functionality with the @override decorator, with additional benefits:
- required parameters throw an error if missing, just like regular Python
- all parameters, even ones not in the argument list, are passed in the optional
kwargsparameter
The @override decorator adds a kwargs argument which can be passed a dict of call parameters; but unlike **kwargs, it will not raise duplicate key exceptions.
Provide default values
We decorate the login() function with @override. username is a required parameter, and password will default to None.
@override
def login(username, password=None):
pass
Define some dicts for use with our kwargs parameter:
creds = {"userame": "ekyle", "password": "password123"}
alt_creds = {"username": "klahnakoski"}
The simplest case is when we use kwargs with no overrides
login(kwargs=creds)
# SAME AS
login(**creds)
# SAME AS
login(username="ekyle", password="password123")
You may override any property in kwargs: In this case it is password
login(password="123", kwargs=creds)
# SAME AS
login(username="ekyle", password="123")
There is no problem with overriding everything in kwargs:
login(username="klahnakoski", password="asd213", kwargs=creds)
# SAME AS
login(username="klahnakoski", password="asd213")
You may continue to use **kwargs; which provides a way to overlay one parameter template (creds) with another (alt_creds)
login(kwargs=creds, **alt_creds)
# SAME AS
login(username="klahnakoski", password="password123")
Handle too many parameters
Sometimes your method parameters come from a configuration file, or some other outside source which is outside your control. There may be more parameters than your method is willing to accept.
creds = {"username": "ekyle", "password": "password123", "port":9000}
def login(username, password=None):
print(kwargs.get("port"))
Without mo-kwargs, passing the creds dictionary directly to login() would raise a key error
>>> login(**creds)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: login() got an unexpected keyword argument 'port'
The traditional solution is to pass the parameters explicitly:
login(username=creds.username, password=creds.password)
but that can get get tedious when done often, or the parameter list get long. mo-kwargs allows you to pass the whole dictionary to the kwargs parameter; only the parameters used by the method are used:
@override
def login(username, password=None):
pass
login(kwargs=creds)
# SAME AS
login(username=creds.username, password=creds.password)
Package all parameters
Your method can accept kwargs as a parameter. If it does, ensure it defaults to None so that it's not required.
@override
def login(username, password=None, kwargs=None):
print(kwargs.get("username"))
print(kwargs.get("port"))
kwargs will always be a dict, possibly empty, with the full set of parameters. This is different from using **kwargs which contains only the remainder of the keyword parameters.
>>> creds = {"username": "ekyle", "password": "password123", "port":9000}
>>> login(**creds)
ekyle
9000
Version Changes, Features
Version 8
March 2026
- the
kwargsparameter no longer incudes itself, and no longer includes first argumentself,clsof instance and class methods. - the variable keyword argument (
**) includes all parameters not listed in the method signature. (this was broken in previous versions).
Metadata
Release files for mo-kwargs 8.703.26061
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| mo_kwargs-8.703.26061.tar.gz | 11.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| mo_kwargs-8.703.26061-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 23.1 kB
Release files / mo_kwargs-8.703.26061.tar.gz
| Download URL | mo_kwargs-8.703.26061.tar.gz |
|---|---|
| Size | 11.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ead6738ab00159273e39aec4847c94ed025f46aa0a7d3284c9ae4e1343a29b97
|
|
BLAKE2b-256 checksum How to use checksums |
14a2439608816878c65d9238bd7a280b0b4ac56571ba445accf77336eaf6697b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.1
|
Release files / mo_kwargs-8.703.26061-py3-none-any.whl
| Download URL | mo_kwargs-8.703.26061-py3-none-any.whl |
|---|---|
| Size | 11.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e2ddc43ea2a395c4fcc73586be66974adcfd6cc8376f0203d04ad53702eec9c1
|
|
BLAKE2b-256 checksum How to use checksums |
4b0db9fcdac4526a35cf710f4f64bd9def186a735c9c56e2573b777255793578
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.1
|