Very small package that offers a metaclass to automatically safeguard mutable function arguments, preventing them from being modified.
Project description
Immutable defaults arguments.
This module provides facilities for turning mutable default function arguments
into immutable ones.
You can install this package with the standard ``pip`` command::
$ pip install immutable_default_args
The issue with `mutable argument default values<http://stackoverflow.com/questions/1132941/least-astonishment-in-python-the-mutable-default-argument>`_ 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 *SO* 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]
...
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 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]
...
This module provides facilities for turning mutable default function arguments
into immutable ones.
You can install this package with the standard ``pip`` command::
$ pip install immutable_default_args
The issue with `mutable argument default values<http://stackoverflow.com/questions/1132941/least-astonishment-in-python-the-mutable-default-argument>`_ 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 *SO* 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]
...
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 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]
...
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
Built Distribution
Close
Hashes for immutable_default_args-0.0.2.zip
Algorithm | Hash digest | |
---|---|---|
SHA256 | 19d356dfc0b688a020739a50549d1186a4c54d4337bed641a901901676a9b0d6 |
|
MD5 | bb00fff0ab798b6967512040628aa320 |
|
BLAKE2b-256 | aed7c1217da8bf757d8fb9a8eacf2680b31315ecc9f766aced8a0aea8803591e |
Close
Hashes for immutable_default_args-0.0.2-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8c74af3f8d1bce1d4cfa688df68f5307376e4f282a164f1a6e740fd872e0db2b |
|
MD5 | 0dbbde04569a5356c34438a496edbc18 |
|
BLAKE2b-256 | b0ec8125b82078e6c6d67d66fd9a478f8e504bfc058d7ab2c773bed56364a168 |