Skip to main content

Immutable default arguments

PyPI Package PyPI Python Versions PyPI Package License Current build status for Travis CI Current build status for AppVeyor Code Health

This module provides facilities for turning mutable default function arguments into immutable ones. It is fairly lightweight and has no non-standard dependencies. You can install this package with the standard pip command:

$ pip install immutable_default_args

The issue with mutable argument default values is pretty well known in Python. Basically mutable default values are assigned once at define time and can then be modified within the function body which might come as a surprise. Here is the example from the stackoverfow thread:

def foo(a=[]):
    a.append(5)
    return a

>>> foo()
[5]
>>> foo()
[5, 5]
>>> foo()
[5, 5, 5]
...

The default way of preventing this behaviour is to use None as the default and check for it in the function body, like so:

def foo(a=None):
    a = a if (type(a) is list) else []
    a.append(5)
    return a

>>> foo()
[5]
>>> foo()
[5]
...

Usage

This package aims to offer two additional options to fix this issue:

  • With a handy function decorator @fix_mutable_kwargs to fix a certain function.

  • With a metaclass ImmutableDefaultArguments to fix all methods, classmethods and staticmethods at once.

Using the decorator:

from immutable_default_args import fix_mutable_kwargs

@fix_mutable_kwargs
def foo(a=[]):
    a.append(5)
    return a

>>> foo()
[5]
>>> foo()
[5]
...

It doesn’t matter if the iterable is empty or not:

@fix_mutable_kwargs
def foo(a=[1, 2, {'key': 'value'}, 3, 4]):
    a.append(5)
    return a

>>> foo()
[1, 2, {'key': 'value'}, 3, 4, 5]
>>> foo()
[1, 2, {'key': 'value'}, 3, 4, 5]
...

Fixing all mutable default values for all methods of an object via the ImmutableDefaultArguments metaclass:

class Foo(object):

    __metaclass__ = ImmutableDefaultArguments  # Py2 syntax

    def foo(self, a=[]):
        a.append(5)
        return a

    @classmethod  # staticmethods work as well
    def foo_classmethod(cls, a=[]):
        a.append(5)
        return a

instance_of_foo = Foo()
>>> instance_of_foo.foo()
[5]
>>> instance_of_foo.foo()
[5]
...
>>> Foo.foo_classmethod()
[5]
>>> Foo.foo_classmethod()
[5]

Compatibility

The immutable_default_args package is tested against Py2/3 and is supported from Py2.7 upstream.

Changelog

0.0.8 (08.05.2016)

  • Add .landscape.yml and badges and more jazz.

0.0.7 (08.05.2016)

  • Add CI and badges and all that jazz.

0.0.5 (08.05.2016)

  • Fixed documentation

0.0.2 (08.05.2016)

  • Added @fix_mutable_kwargs decorator

  • Refactorings/Cleanup

0.0.1 (08.05.2016)

  • First release. Included only ImmutableDefaultArguments metaclass

License

You are free to do whatever you like with the code. Please note that I am not accountable for anything that might have happened as a result of executing the code from the immutable_default_args package….ever.

Metadata

Release files for immutable_default_args 0.0.9

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

Source distribution (sdist)

Source distribution for immutable_default_args 0.0.9
File Size Uploaded Provenance Actions
immutable_default_args-0.0.9.zip 9.7 kB Without provenance Details for immutable_default_args-0.0.9.zip

Built distribution (wheel)

Table of built distributions (wheels) for immutable_default_args 0.0.9
File Interpreter ABI Platform Actions
immutable_default_args-0.0.9-py2.py3-none-any.whl Python 3, Python 2 none any Details for immutable_default_args-0.0.9-py2.py3-none-any.whl

Total release size: 18.8 kB

Release files / immutable_default_args-0.0.9.zip

Download URL immutable_default_args-0.0.9.zip
Size 9.7 kB
Tags Source
SHA-256 checksum
How to use checksums
eb7bec293c4041c00e465a5c4ba63d16f25910cf21b3689c9476d4d93d39d80d
BLAKE2b-256 checksum
How to use checksums
2f78ca0bb2d629c253e4da283169dd813efb7408d16075dc38055835ebd3bc17
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / immutable_default_args-0.0.9-py2.py3-none-any.whl

Download URL immutable_default_args-0.0.9-py2.py3-none-any.whl
Size 9.1 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
04cee62099c0e77bc54517fd2754cbdc83ef1305c292209a6933784dea0c7203
BLAKE2b-256 checksum
How to use checksums
0b288bb34cbc27a39399b6c427fafa741283362914d28cfdb14445c913fe8ab4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for immutable_default_args 0.0.9

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.

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