Skip to main content

Introduction

stabledefaults is a small package containing a decorator to allow for the expected behavior of lists, dicts and other mutable arguments in default arguments.

Explanation

In Python, functions (as anything else) are objects, and the default arguments are stored as attributes that are initialized in definition.

Once this is known and the person understands that variables are references in Python, then it is relatively straightforward to understand the following behavior:

def f(x=[]):
    x.append(2)
    return x
>>> a = f()
>>> a
[2]
>>> f()
>>> a
[2, 2]

Nevertheless, this is unintuitive. Not only that, but dealing with this requires things such as

def f(x = None):
    if x is None:
        x = []

    x.append(2)
    return x

which forces types such as list | None where just list should suffice, and also forces code inside the function itself.

This package solves this issue with a decorator. For instance, the example above would become

@stabledefaults()
def f(x=[]):
    x.append(2)
    return x
>>> a = f()
>>> a
[2]
>>> f()
>>> a
[2]

Release files for stabledefaults 0.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for stabledefaults 0.1.4
File Size Uploaded
stabledefaults-0.1.4.tar.gz 3.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for stabledefaults 0.1.4
File Interpreter ABI Platform
stabledefaults-0.1.4-py3-none-any.whl Python 3 none any Details

Total release size: 6.4 kB

Release files / stabledefaults-0.1.4.tar.gz

Download URL stabledefaults-0.1.4.tar.gz
Size 3.0 kB
Tags Source
SHA-256 checksum
How to use checksums
2518225f2103ea801d0148fba07bc414c79f4552f38a718b0cc0a3c224e14432
BLAKE2b-256 checksum
How to use checksums
757a6ac076b8136478aaf5c85909706e614f1d0c0701b0f9aeb8b64a50715757
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / stabledefaults-0.1.4-py3-none-any.whl

Download URL stabledefaults-0.1.4-py3-none-any.whl
Size 3.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a800d58de18645a8036d2b4f673abc4549105d335abe414faa498dcdb6104bbe
BLAKE2b-256 checksum
How to use checksums
6381111a92d53483e76fad38b00d69ffc291cd9b41e59cfe92f6dd00991d621e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release history Release notifications | RSS feed

This release

0.1.4 This release

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page