Skip to main content

Late allows for late binding of default arguments

Reason this release was yanked:

outdated

Project description

Late

Late binding for Python default arguments

What is it?

Late provides decorators and functions to work around the issues that early binding of default argument values produces in Python.

What follows is not intuitive for newcomers to Python, but it's something that everyone learns quickly:

>>> def f(x=[]):
...     x.append(1)
...     return x
...
>>> f()
[1]
>>> f()
[1, 1]
>>> f()
[1, 1, 1]

The behavior in Python is that the same [] initializer value is passed on every function invocation.

The coding pattern to work around the above is to use None as the initializer, and check for the parameter value at the start of the function code:

>>> def f(x=None):
...     if x is None:
...         x = []
...     x.append(1)
...     return x
...
>>> f()
[1]
>>> f()
[1]
>>> f()
[1]

It's ugly, but it works.

Now comes the other ugly part. When using type annotations, the above function must be declared in away so that type checkers do not complain about using None as the default value:

>>> def f(x: list[Any] | None = None) -> list[Any]:

Late provides a way to solve the above ugliness with some decorator magic. This is how the code looks with some of that magic:

from late import latebinding, __


@latebinding
def f(x: list[Any] = __([])) -> list[Any]:
    x.append(1)
    return x

assert f() == [1]
assert f() == [1]
assert f() == [1]

Working with classes

Late also works with classes and dataclass. The @latebinding decorator must be the outer one:

@latebinding
@dataclass
class C:
    x: list[Any] = __([])  # noqa

c = C()
assert c.x == []

d = C()
assert d.x == []
c.x = [1]
assert c.x == [1]
assert d.x == []

assert d.x is not c.x

Working with generators

Late allows passing a generator as a default argument value, and it will provide the next value on each function call. The usefulness of this feature is unknown, but it's something that came up during the discussions about default arguments, so Late implements it.

    def fib() -> Iterator[int]:
        x, y = 0, 1
        while True:
            yield x
            x, y = y, x + y


    @latebinding
    def f(x: int = __(fib())) -> int:
        return x

    assert f() == 0
    assert f() == 1
    assert f() == 1
    assert f() == 2
    assert f() == 3
    assert f() == 5

About name choice

The names of what Late exports are chosen to be explicit where it matters, and to not get in the way of the visuals of a declaration. In particular, __() was chosen to interfere the least possible with reading a function declaration (late() is another name for it, and __ is seldom used in Python code).

At any rate, Late is so simple and so small that you can apply any changes you like and use it as another part of your code instead of installing it as a library.

License

Late is under the GNU GENERAL PUBLIC LICENSE Version 3, as reads in the LICENSE file.

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-1.0.2.tar.gz (41.3 kB view details)

Uploaded Source

Built Distribution

Late-1.0.2-py3-none-any.whl (29.4 kB view details)

Uploaded Python 3

File details

Details for the file Late-1.0.2.tar.gz.

File metadata

  • Download URL: Late-1.0.2.tar.gz
  • Upload date:
  • Size: 41.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.0

File hashes

Hashes for Late-1.0.2.tar.gz
Algorithm Hash digest
SHA256 e4a7cae6a24da685e22c8b968765ed61cbdb650e97eec5b707f225d423fd21de
MD5 a0f99ed8937ebc79bc7150ce87c55cc4
BLAKE2b-256 8d98ae280735c902e04307feda07da2af8b61fce6bc4a94dfef3059bfa20759c

See more details on using hashes here.

File details

Details for the file Late-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: Late-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 29.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.0

File hashes

Hashes for Late-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4a99d11084c24421df54a7b1ce44c19c3e95d96e34aa42ec4ee72018e40bbcc7
MD5 1609d38ac1123c778db2aef6dcb75c54
BLAKE2b-256 4a0c801f6c56e4d7330db30f61c800bbb54ba8d2359f1bb7e77b520ed54f946c

See more details on using hashes here.

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