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
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.1.tar.gz
.
File metadata
- Download URL: Late-1.0.1.tar.gz
- Upload date:
- Size: 40.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.12.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 37d9fe3b91bc40f9a5fd7c37fbe82d50f5add249a70d71df873e5d5314ebf7c0 |
|
MD5 | 1573c4c332cb4e3371e1a01a71c704ac |
|
BLAKE2b-256 | 923c65a12c4fcd971fa544a6f53e85767fc02316e29c50a39cd30274f54f8f56 |
File details
Details for the file Late-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: Late-1.0.1-py3-none-any.whl
- Upload date:
- Size: 28.7 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 | b3212b4f3c1ca6b33958faf8e6d4958ec000ff815c714554ffef854fc3230de7 |
|
MD5 | 8558ebe6d3ead876f9ec8dafa4cbbda1 |
|
BLAKE2b-256 | c12ebe271afde07377be06991443335613c496df425bd792034362b2a92b90ba |