Allows evaluating default arguments at function-call time
Project description
Late-Bound Arguments
This package tries to emulate the behaviour of syntax proposed in PEP 671 via a decorator.
Usage
Mention the names of the arguments which are to be late-bound in the arguments of the delay
decorator, and their default values as strings in the function signature.
Mutable default arguments:
from late_bound_arguments import delay
@delay("my_list")
def foo(my_list="[]"):
my_list.append(1)
return my_list
print(foo()) # [1]
print(foo()) # [1]
print(foo([1, 2, 3])) # [1, 2, 3, 1]
Referencing previous arguments:
from late_bound_arguments import delay
@delay("my_list", "n")
def foo(my_list="[1, 2]", n: int = "len(my_list)"):
return my_list, n
print(foo()) # ([1, 2], 2)
print(foo([1, 2, 3])) # ([1, 2, 3], 3)
Additionally, the function signature is not overwritten, so help(foo)
will provide the original signature retaining the default values.
Reasoning
Mutable defaults are often tricky to work with, and may not do what one might naively expect.
For example, consider the following function:
def foo(my_list=[]):
my_list.append(1)
return my_list
print(foo())
print(foo())
One might expect that this prints [1]
twice, but it doesn't. Instead, it prints [1]
and [1, 1]
.
A new list object is not created every time foo
is called without providing the b
argument: the list is only created once - when the function is defined - and the same list object is used for every call. As a result, if it is mutated in any call, the change is reflected in subsequent calls.
A common workaround for this is using None
as a placeholder for the default value, and replacing it inside the function body.
def foo(my_list=None):
if my_list is None:
my_list = []
...
In case None
is a valid value for the argument, a sentinel object is created beforehand and used as the default instead.
_SENTINEL = object()
def foo(my_list=_SENTINEL):
if my_list is _SENTINEL:
my_list = []
...
However, this solution, apart from being unnecessarily verbose, has an additional drawback: help(foo)
in a REPL will fail to inform of the default values, and one would have to go through the source code to find the true signature.
Project details
Release history Release notifications | RSS feed
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 late_bound_arguments-0.1.2.tar.gz
.
File metadata
- Download URL: late_bound_arguments-0.1.2.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.11 CPython/3.10.0 Windows/10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 639c05d8b3f7c8e131986882b108078cad5c82229ca68cf087090b70afc184f0 |
|
MD5 | 5913ea07a0976c5857e4078e944e2e5b |
|
BLAKE2b-256 | 8cef21dd0dd8403e85ba1af417713e27c25f36ce63a2fde89371caf0f287ec27 |
File details
Details for the file late_bound_arguments-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: late_bound_arguments-0.1.2-py3-none-any.whl
- Upload date:
- Size: 3.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.11 CPython/3.10.0 Windows/10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4223c17dbd5be0ae2f51fe53505534ae852b5bc1f81e0ad6486dcee3e26675ad |
|
MD5 | 86120eb3485c77cc5beffd18de499d27 |
|
BLAKE2b-256 | 3dde69eeb4eff8c9b139a5214a2be684d3d7f0316c1009c5f3fcea869647ca38 |