Skip to main content

Helper decorator to lock default mutable values

Project description

lock-defaults

PyPI License Copy and Paste

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.

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

lock-defaults-0.1.3.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

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

lock_defaults-0.1.3-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file lock-defaults-0.1.3.tar.gz.

File metadata

  • Download URL: lock-defaults-0.1.3.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.10.2 Linux/5.13.0-1025-azure

File hashes

Hashes for lock-defaults-0.1.3.tar.gz
Algorithm Hash digest
SHA256 f45d64a6e1e0399dcf5b0872d6832af4c82836a8ac1cc75b9d11248f462193df
MD5 204d7baeefcac08a93eb836d90ad8e0a
BLAKE2b-256 c0ceab1164f2da8d2e5df584677b38d9079fea73bec8a527ff4b058310943921

See more details on using hashes here.

File details

Details for the file lock_defaults-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: lock_defaults-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.10.2 Linux/5.13.0-1025-azure

File hashes

Hashes for lock_defaults-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 663e53d1a64537714e5947358e19b1ae7de6c81a7780889687c09d9c8478cb02
MD5 a08b33fa1a96085ce4ad1e4d9dbcf9e1
BLAKE2b-256 b5afe8a6b06e1747e2a4ed0ea2f9860d6a5c5b7d5dad1a911261d36da282b41c

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