Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

late_bound_arguments-0.1.1.tar.gz (3.8 kB view hashes)

Uploaded Source

Built Distribution

late_bound_arguments-0.1.1-py3-none-any.whl (3.7 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page