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
unique_defaults-0.1.tar.gz
(3.5 kB
view hashes)
Built Distribution
Close
Hashes for unique_defaults-0.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 192955f510fb2cbfb8d44a3371bcd7f8d9b05976bdbb9a40e7d2e79bf5817771 |
|
MD5 | 55974bda1884dae9304d244b1576570b |
|
BLAKE2b-256 | 91a5f7aae66aa9576b52cf21b11a3cbe770924dfef1e016974d99d0c82c4afc0 |