Python dynamic default arguments.
Project description
Python Dynamic Default Arguments
This package provide facilities to make default arguments of python's functions dynamic.
Context
This code is extracted from another project of mine. It solves a problem that was also mentioned in this stackoverflow thread.
It turns out that changing functions' default arguments in an elegant way is a lot harder than what we think. The common approach is to define a function that retrieves the value of the default argument stored somewhere:
class _empty(type):
pass # placeholder
B = 'path/to/heaven'
def get_default_b():
# method that
return B
def foo(a, b=_empty):
if b is _empty:
b = get_default_b()
send_to(a, destination=b) # do something
def main():
global B
B = 'path/to/hell'
foo('Putin')
The old standard way is certainly the best, but programmers should also be aware of numbers of function calls when there are many arguments to be made dynamically default. But the point is, it doesn't look nice.
This module's solution limits to a single wrapper function, which is compile
d from string for minimal object
initialization and condition checking, so most of the overheads are during module importing.
Requirements
- Python 3
Installation
dynamic-default-args is available on PyPI, this is a pure Python package.
pip install dynamic-default-args
Usage
This package provides two components:
named_default
: A object that has a name and contains a value. This is a singleton-like object, which means any initialization with the same name will return the first one with the same registered name.dynamic_default_args
: A function decorator for substituting any givennamed_default
with its value when function is called.
Creating a named_default
:
There are 2 ways to initialize a named_default
, either pass a pair of named_default(name, value)
positional
arguments or use a single keyword argument named_default(name=value)
.
from dynamic_default_args import named_default
# method 1
x = named_default('x', 1)
# method 2
y = named_default(y=1)
It is not necessary to keep the reference of this object as you can always recover them when calling named_default
again with the same name.
from dynamic_default_args import named_default
print(named_default('x').value)
named_default('x').value = 1e-3
Decorating function with dynamic_default_args
:
Here is an example in example.py
:
from dynamic_default_args import dynamic_default_args, named_default
# Note that even non-dynamic default args can be formatted because
# both are saved for populating positional-only defaults args
@dynamic_default_args(format_doc=True)
def foo(a0=named_default(a0=5),
a1=3,
/,
a2=named_default(a2=1e-2),
a3=-1,
*a4,
a5=None,
a6=named_default(a6='python')):
"""
A Foo function that has dynamic default arguments.
Args:
a0: Positional-only argument a0. Dynamically defaults to a0={a0}.
a1: Positional-only argument a1. Defaults to {a1}.
a2: Positional-or-keyword argument a2. Dynamically defaults to a2={a2}.
a3: Positional-or-keyword argument a3. Defaults to {a3}
*a4: Varargs a4.
a5: Keyword-only argument a5. Defaults to {a5}.
a6: Keyword-only argument a6. Dynamically defaults to {a6}.
"""
print('Called with:', a0, a1, a2, a3, a4, a5, a6)
# test output:
foo()
# Called with: 5 3 0.01 -1 () None python
By passing format_doc=True
, the decorator will try to bind default values of argument with names defined in format
keys of the docstring.
Any modification to the dynamic default values will update the docstring with an event.
named_default('a6').value = 'rust'
help(foo)
Output:
foo(a0=5, a1=3, /, a2=0.01, a3=-1, *a4, a5=None, a6='rust')
A Foo function that has dynamic default arguments.
Args:
a0: Positional-only argument a0. Dynamically defaults to a0=5.
a1: Positional-only argument a1. Defaults to 3.
a2: Positional-or-keyword argument a2. Dynamically defaults to a2=0.01.
a3: Positional-or-keyword argument a3. Defaults to -1
*a4: Varargs a4.
a5: Keyword-only argument a5. Defaults to None.
a6: Keyword-only argument a6. Dynamically defaults to rust.
Binding
The named_default
object will emit an event to all registered listeners, which can be set by calling .connect
method:
from dynamic_default_args import named_default
variable = named_default('my_variable', None)
def on_variable_changed(value):
print(f'Changed to {value}')
variable.connect(on_variable_changed)
Limitations
This solution relies on function introspection provided by the inspect
module, which does not work on built-in
functions (including C/C++ extensions).
However, you can wrap them with a python with, or modify the source code of the decorator to accept a custom signature
as argument.
For Cython users, a def
or cpdef
(might be inspected incorrectly) function defined in .pyx
files can be
decorated by setting binding=True
.
import cython
from dynamic_default_args import dynamic_default_args, named_default
@dynamic_default_args(format_doc=True)
@cython.binding(True)
def add(x: float = named_default(x=0.),
y: float = named_default(y=0.)):
"""``cython.binding`` will add docstring to the wrapped function,
so we can format it later.
Args:
x: First argument, dynamically defaults to {x}
y: Second argument, dynamically defaults to {y}
Returns:
The sum of x and y
"""
return x + y
Further improvements:
Some parts of the project can be converted to Cython, including the wrapper function to make use of typed data ( which I have already done for cy-root), but the difference is negligible.
License
The code is released under MIT-0 license. See LICENSE.txt
for details.
Feel free to do anything, I would be surprised if anyone does use this 😐.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file dynamic-default-args-0.0.1.tar.gz
.
File metadata
- Download URL: dynamic-default-args-0.0.1.tar.gz
- Upload date:
- Size: 14.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 44bc747efbc0acb00b33e5a285c10a186d7c3c9c9b726fb9eea27216ed41ed8e |
|
MD5 | 7fd610a6ad769944c377ba35906d7f12 |
|
BLAKE2b-256 | 0d57b6baf529dd85b725f30127364cce9493025eb1eff95f057b7dcb9fff6b84 |
File details
Details for the file dynamic_default_args-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: dynamic_default_args-0.0.1-py3-none-any.whl
- Upload date:
- Size: 9.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8127f1c7d024e0042ef0008879a6a0818ad8121ccde65937ae52c52217ccc171 |
|
MD5 | 06279089aac2a1c34ab15830a69a6ed6 |
|
BLAKE2b-256 | b6583b63616859d4b6e6156312d4247400514ccf8340a0ab4e36df148563fb22 |