Python Dynamic Default Arguments
Shared defaults you can update, and factories evaluated for each omitted argument. Pure Python, with no runtime dependencies.
Supports Python 3.8–3.15 and Cython 3.
Installation
From PyPI:
python -m pip install dynamic-default-args
Or from a local checkout (Python 3.9+):
python -m pip install .
Shared Defaults
from dynamic_default_args import default, default_factory, dynamic_default_args
timeout = default(5)
@dynamic_default_args
def request(url, timeout=timeout):
return url, timeout
assert request("/status") == ("/status", 5)
timeout.value = 10
assert request("/status") == ("/status", 10)
assert request("/status", None) == ("/status", None)
Only omitted arguments resolve. Explicit values, including None, pass through.
A default cell shares its value; use a factory when each call needs a fresh object.
Factories
default_factory computes a value per omission. Its optional depends_on names
earlier parameters, passed to the factory in the given order:
@dynamic_default_args()
def take(items, limit=default_factory(len, depends_on=("items",)), *,
output=default_factory(list)):
output.extend(items[:limit])
return output
assert take([1, 2, 3]) == [1, 2, 3]
assert take([1, 2, 3], 1) == [1]
Factories are synchronous and resolve from left to right. default(callable)
passes the callable itself; default_factory(callable) invokes it.
Methods
Instance, class, and static methods, inheritance, and bound methods are supported.
Either decorator order works with classmethod and staticmethod.
class Client:
timeout = 5
@dynamic_default_args
def request(self, timeout=default_factory(lambda self: self.timeout,
depends_on=("self",))):
return timeout
client = Client()
assert client.request() == 5
client.timeout = 10
assert client.request() == 10
Class-method factories can similarly depend on cls.
Named Defaults
Use named_default to retrieve a shared cell across modules:
from dynamic_default_args import named_default
shared = named_default("myapp.timeout", 5)
assert named_default("myapp.timeout") is shared
named_default("myapp.timeout").value = 10
named_default.unregister("myapp.timeout")
The first registration wins; later initialization values are ignored.
Defaults Outside the Signature
The defaults mapping is useful for typed functions and Cython parameters:
scale = default(2.0)
@dynamic_default_args(defaults={"x": scale})
def compute(x: float = 0.0) -> float:
return x * 2
assert compute() == 4.0
scale.value = 3.0
assert compute() == 6.0
For typed Cython parameters, keep valid native defaults and supply dynamic values
through this mapping. Python wrappers around cpdef are supported; C-level calls
retain native defaults. See the Cython example and
its build-and-run helper.
Other Features
- Preserves positional-only, keyword-only, and variadic argument binding.
- Supports partials, callable instances, and inspectable built-ins.
- Preserves coroutine, generator, and async-generator behavior. Defaults resolve when execution starts, or on first iteration for generators.
format_doc=Trueupdates simple docstring fields such as{timeout}when a shared value changes. Set it toFalseto leave docstrings unchanged.- Shared cells expose
connect(callback),disconnect(callback), anddisconnect_all()for change notifications. signature=inspect.Signature(...)adapts callables without signature metadata.
More runnable Python examples are in examples/factories.py. See CHANGELOG.md for changes. Licensed under MIT-0.
Metadata
Release files for dynamic-default-args 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| dynamic_default_args-0.1.0.tar.gz | 21.1 kB | Without provenance | Details for dynamic_default_args-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| dynamic_default_args-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for dynamic_default_args-0.1.0-py3-none-any.whl |
Total release size: 32.4 kB
Release files / dynamic_default_args-0.1.0.tar.gz
| Download URL | dynamic_default_args-0.1.0.tar.gz |
|---|---|
| Size | 21.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f456f07d2c83c10524217d556e1c5ed2a8fae167f099eb7c77b9d1c6faa53256
|
|
BLAKE2b-256 checksum How to use checksums |
b22dc230a47adfae63c765dd3c3b2cccc8786270fa0603cc05843a19b5ca6d41
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Release files / dynamic_default_args-0.1.0-py3-none-any.whl
| Download URL | dynamic_default_args-0.1.0-py3-none-any.whl |
|---|---|
| Size | 11.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f763c1e5c48eef399f8f39931646b116beadb3939b509abb19594cf9a8dce283
|
|
BLAKE2b-256 checksum How to use checksums |
bf8a2ca039582433bd6eafb84942705d34568e69fbb83401c186e6c25c49021f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Security for dynamic-default-args 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.