Skip to main content

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


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 details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

unique_defaults-0.1-py2.py3-none-any.whl (3.0 kB view details)

Uploaded Python 2Python 3

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

Hashes for unique_defaults-0.1.tar.gz
Algorithm Hash digest
SHA256 d812f76571e5b8257f3eb0ff3c8e92e0820eb05a53f22a9e683dba557b2b42a3
MD5 e640d9e03cefe715e2e7a2b14dd02cf5
BLAKE2b-256 e8756b0cffd62e495429e14515f4de6d0365b31025881798f115ded08880fb1b

See more details on using hashes here.

File details

Details for the file unique_defaults-0.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for unique_defaults-0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 192955f510fb2cbfb8d44a3371bcd7f8d9b05976bdbb9a40e7d2e79bf5817771
MD5 55974bda1884dae9304d244b1576570b
BLAKE2b-256 91a5f7aae66aa9576b52cf21b11a3cbe770924dfef1e016974d99d0c82c4afc0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page