Skip to main content

Makes functions parameters immutable

Project description

Python License MIT Coverage Status Unit Tests Tox Ruff Mypy

Constify

The constify library provides the freezeparams decorator to make mutable default values and arguments passed to functions immutable.

Why use the freezeparams decorator ?

[1] The default value of a parameter is evaluated only once, which can lead to unexpected behavior if the value is a mutable object, such as a list or a dictionary. For example, the following function accumulates arguments across calls:

Example

def f(a, L=[]):
    L.append(a)
    return L

print(f(1))  # [1]
print(f(2))  # [1, 2]
print(f(3))  # [1, 2, 3]

To prevent this default value from being shared, the function can be rewritten as follows:

def f(a, L=None):
    if L is None:
        L = []
    L.append(a)
    return L

This solution is effective, but with multiple mutable default values, it requires many checks. The freezeparams decorator simplifies the use of mutable objects as default values and arguments.

Installation

pip install constify

Usage

from constify import freezeparams

@freezeparams
def f(a, L=[]):
    L.append(a)
    return L

# Default value (list) remains unchanged
print("f(1) =>", f(1))  # [1]
print("f(2) =>", f(2))  # [2]
print("f(3) =>", f(3))  # [3]

print("-" * 25) # just a separator

# Passed list remains intact
x = [1, 2]
print("x =", x)  # [1, 2]
print(f"f(3, {x}) =>", f(3, x))  # [1, 2, 3]
print(f"f(4, {x}) =>", f(4, x))  # [1, 2, 4]
print("x =", x)  # [1, 2]

You can comment out the decorator and run the code again to see the difference.

Simple and effective!

Sources

[1] https://docs.python.org/3.13/tutorial/controlflow.html#default-argument-values

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

constify-0.1.0.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

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

constify-0.1.0-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file constify-0.1.0.tar.gz.

File metadata

  • Download URL: constify-0.1.0.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.13.1

File hashes

Hashes for constify-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c55aa073ab6cd8ba8bb88245f25ce0e130897233f3184073638c544ebea20c33
MD5 0d497e4e3b28eafeaaf19b7422f9a44f
BLAKE2b-256 4d64866d865699e866043177d4b09b99d13b8f3d208b958d213e0d2a614c7517

See more details on using hashes here.

File details

Details for the file constify-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: constify-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.13.1

File hashes

Hashes for constify-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 caaa0c7b5e8fec621df45cf1eb7eb9c2fba89cb4b64067fb76f95ebda4393e9e
MD5 cca9525bbf89820e385728549f00b5a6
BLAKE2b-256 abde5a2017f3b009755754952f19040736a7c06170008af98346587a42235715

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