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 values produces in Python.

This 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 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 orther ugly part. When using type annotations, the above function must be declared in away 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 new code would look with some magic:

from late import latebinding, __


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

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 vissuals of a declaration. In particular, __() was chose to intefere the least possible with reading a function declaration (late() is another name for it).

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 reades in the [LICENSE)(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.0b1.tar.gz (39.6 kB view details)

Uploaded Source

Built Distribution

Late-1.0.0b1-py3-none-any.whl (27.2 kB view details)

Uploaded Python 3

File details

Details for the file Late-1.0.0b1.tar.gz.

File metadata

  • Download URL: Late-1.0.0b1.tar.gz
  • Upload date:
  • Size: 39.6 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.0b1.tar.gz
Algorithm Hash digest
SHA256 dbe8343a00f108c3c45b1ebe3df8ffc8357008a9fe055e192b36620479330472
MD5 24fb80962d49950eaa16ff3a0c48e0ee
BLAKE2b-256 13bf60f3e76630b0e58f3421a748c17dee9bc7017c3e84785629394e4ac36522

See more details on using hashes here.

File details

Details for the file Late-1.0.0b1-py3-none-any.whl.

File metadata

  • Download URL: Late-1.0.0b1-py3-none-any.whl
  • Upload date:
  • Size: 27.2 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.0b1-py3-none-any.whl
Algorithm Hash digest
SHA256 2dc867087d39bb60bd604b291364c93dad571a0e3607ccb294f986cbef7933e0
MD5 d73d629a24d6dee4d71adcc4e71d693f
BLAKE2b-256 8fe422eeec1e934a7700fb4983dae3631d9ab98468bd3b494aa6834fa863ef36

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