Replace a function's default values with objects unique to each function call
Project description
unique_defaults
Replace a function's default values with objects unique to each function call. This allows writing signatures with mutable defaults without sharing the underlying object between funciton calls.
from unique_defaults import unique_lists
def classic(a=[]):
a.append("again")
return " ".join(a)
classic() == "again"
classic() == "again again"
@unique_lists
def unique(a=[]):
a.append("again")
return " ".join(a)
unique() == "again"
unique() == "again"
Using the mutable object directly in the function signature leads to shorter and more accurate annotations, as well as simplifying the function's logic.
from typing import List, Optional
from unique_defaults import unique_lists
def classic(a: Optional[List] = None):
if a is None:
a = []
a.append("again")
return " ".join(a)
classic() == again
classic() == again
@unique_lists
def unique(a: List = []):
a.append("again")
return " ".join(a)
unique() == again
unique() == again
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file unique_defaults-0.1.tar.gz.
File metadata
- Download URL: unique_defaults-0.1.tar.gz
- Upload date:
- Size: 3.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.26.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d812f76571e5b8257f3eb0ff3c8e92e0820eb05a53f22a9e683dba557b2b42a3
|
|
| MD5 |
e640d9e03cefe715e2e7a2b14dd02cf5
|
|
| BLAKE2b-256 |
e8756b0cffd62e495429e14515f4de6d0365b31025881798f115ded08880fb1b
|
File details
Details for the file unique_defaults-0.1-py2.py3-none-any.whl.
File metadata
- Download URL: unique_defaults-0.1-py2.py3-none-any.whl
- Upload date:
- Size: 3.0 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.26.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
192955f510fb2cbfb8d44a3371bcd7f8d9b05976bdbb9a40e7d2e79bf5817771
|
|
| MD5 |
55974bda1884dae9304d244b1576570b
|
|
| BLAKE2b-256 |
91a5f7aae66aa9576b52cf21b11a3cbe770924dfef1e016974d99d0c82c4afc0
|