unique_defaults
Guarantee new objects are used for each function call.
This allows writing signatures with mutable defaults without sharing the underlying object between function 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"
Individual arguments can be targeted, instead of a general type.
from unique_defaults import unique_defaults
@unique_defaults('line_list')
def log_list(line_list=[], _running_log=[]):
line_list[:0] = ["NOTICE:"]
line = " ".join(line_list)
_running_log.append(line)
return _running_log
log_list(["first"]) == ["NOTICE: first"]
log_list(["second"]) == ["NOTICE: first", "NOTICE: second"]
Any types can be made unique
from unique_defaults import unique_defaults
class Count:
def __init__(self):
self.count = 0
def add(self, n):
self.count += n
def classic(a=Count()):
a.add(1)
return a
classic().count == 1
classic().count == 2
@unique_defaults(Count)
def unique(a=Count()):
a.add(1)
return a
unique().count == 1
unique().count == 1
Metadata
Release files for unique_defaults 1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| unique_defaults-1.0.tar.gz | 4.6 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| unique_defaults-1.0-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details |
Total release size: 8.1 kB
Release files / unique_defaults-1.0.tar.gz
| Download URL | unique_defaults-1.0.tar.gz |
|---|---|
| Size | 4.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1614fa1fbd53b8a52aef201d55524aa25e701d5c289d9ed5a9dfaaa7c68354b1
|
|
BLAKE2b-256 checksum How to use checksums |
f6ee6ed229ff3c595b434e1c83eeb031e9d8014df38028aa040382800a4910dc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-requests/2.27.1
|
Release files / unique_defaults-1.0-py2.py3-none-any.whl
| Download URL | unique_defaults-1.0-py2.py3-none-any.whl |
|---|---|
| Size | 3.5 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
72deacff135e375b7efea7243d7743766ab066ca0c45d0e561eca76a56afae9a
|
|
BLAKE2b-256 checksum How to use checksums |
ef0a6914bee0436dd90be9ab673c402d51eefd1b3cba272fa216d8482e521b43
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-requests/2.27.1
|
Security for unique_defaults 1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.