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]
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).
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
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-1.0.0.tar.gz
.
File metadata
- Download URL: Late-1.0.0.tar.gz
- Upload date:
- Size: 40.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.12.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ce988b322daa21bac8c8e76404a46a0fcc1996ad21e236f872af928f71068fc6 |
|
MD5 | 86823906c6660e0a41e96faac640478c |
|
BLAKE2b-256 | fd15043d2784f789a6df12313d0d189834ccc4da68f08b45551e1b335410d424 |
File details
Details for the file Late-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: Late-1.0.0-py3-none-any.whl
- Upload date:
- Size: 28.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.12.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7b6421eed6aa009399cd21b161a45519c368eaa038afc17c45d198596f7e2d97 |
|
MD5 | 9430a4c2332d2bc5a05f602334305928 |
|
BLAKE2b-256 | 5ead1488cbf9f17bcf4754ea5107d859ac07603123851676c5b6092c4d083813 |