lock-defaults
This decorator is small and only relies on the standard library, so can just be copied into a project.
Python has a weird behaviour around default values for functions. If you use an empty list as a default argument, things added to the list during the function can hang around for next time the function is called. A common pattern of dealing with this is the following:
def func(foo = None):
if foo is None:
foo = []
But this looks rubbish! And gets worse when you add typing:
def func(foo: list | None = None):
if foo is None:
foo = []
You don't need that workaround for any other of default value. Why does the list parameter have to pretend it can be None, when that's not the intention at all?
The lockmutabledefaults decorator fixes this by introducing what should be the default approach, and default values that are lists, dictionaries or sets are isolated in each re-run.
@lockmutabledefaults
def func(foo: list = []):
pass
Install
You can install from pip: python -m pip install lock-defaults
Or you can copy the function directly into your projects.
- For python 3.10+: with_paramspec/lock_defaults.py
- For python 3.8, 3.9: without_paramspec/lock_defaults.py
Release files for lock-defaults 0.1.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| lock-defaults-0.1.4.tar.gz | 3.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| lock_defaults-0.1.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 9.7 kB
Release files / lock-defaults-0.1.4.tar.gz
| Download URL | lock-defaults-0.1.4.tar.gz |
|---|---|
| Size | 3.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
49b0e29bcd7d752e119429983255a45edde5b71b70addca95f7f3a1f71635f3a
|
|
BLAKE2b-256 checksum How to use checksums |
160b94c5f103d2f109e1b4d070166d4241cec49aedd3c036d37fce864e9b47ee
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.13 CPython/3.10.2 Linux/5.13.0-1025-azure
|
Release files / lock_defaults-0.1.4-py3-none-any.whl
| Download URL | lock_defaults-0.1.4-py3-none-any.whl |
|---|---|
| Size | 6.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3d8977663c66bce355e8e6db000d9ce4cd79bf1b903d5a92848c5b1406005e27
|
|
BLAKE2b-256 checksum How to use checksums |
db0fe616bae690569a24983593ab5cd3c0582946abf663c2f57d543d1c8b8bf5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.13 CPython/3.10.2 Linux/5.13.0-1025-azure
|