Skip to main content

Default Mutable

Prevent mutable default value behavior

Default Mutable is a Python decorator that overrides default mutable values and prevents object mutation from one call to another. More info on https://docs.python-guide.org/writing/gotchas/

Features

  • Easy to use
  • Preset default value

Advantages

  • Highly flexible
  • Lightweight and independent
  • Open-source
  • Real use cases
  • Support & documentation

Authors

  • Rudy Fernandez

Install

The easiest way to install default_mutable using pip: pip install default_mutable

Examples

Python’s default arguments are evaluated once when the function is defined, not each time the function is called (like it is in say, Ruby). This means that if you use a mutable default argument and mutate it, you will and have mutated that object for all future calls to the function as well.

def function1(idx, dict1 = {}, list1 = [], element = 'a'):
    list1.append(element)
    dict1[f'{element}{idx}'] = f'{element}{idx}'
    print('', idx, dict1, list1)

print('')
print('function1:')
function1(idx= 1)   # 1 {'a1': 'a1'} ['a']
function1(idx= 2)   # 2 {'a1': 'a1', 'a2': 'a2'} ['a', 'a']

Setting all the arguments with mutable default arguments to an EMPTY dictionary or list

from default_mutable.DefaultMutable import defaultMutable

@defaultMutable
def function2(idx, dict1 = {}, list1 = [], element = 'a'):
    list1.append(element)
    dict1[f'{element}{idx}'] = f'{element}{idx}'
    print('', idx, dict1, list1)

print('')
print('function2:')
function2(1)    # 1 {'a1': 'a1'} ['a']
function2(2)    # 2 {'a2': 'a2'} ['a']

Setting all the arguments with mutable default arguments to an EMPTY dictionary or list

from default_mutable.DefaultMutable import defaultMutable

@defaultMutable
def function2_2(idx, dict1 = {}, list1 = ['list_init'], element = 'a'):
    list1.append(element)
    dict1[f'{element}{idx}'] = f'{element}{idx}'
    print('', idx, dict1, list1)

print('')
print('function2_2:')
function2_2(1)    # 1 {'a1': 'a1'} ['a']
function2_2(2)    # 2 {'a2': 'a2'} ['a']

When the decorator is executed with not arguments, we get the default behavior (same as "function1")

from default_mutable.DefaultMutable import defaultMutable

@defaultMutable()
def function3(idx, dict1 = {}, list1 = [], element = 'a'):
    list1.append(element)
    dict1[f'{element}{idx}'] = f'{element}{idx}'
    print('', idx, dict1, list1)

print('')
print('function3:')
function3(idx= 1)   # 1 {'a1': 'a1'} ['a']
function3(idx= 2)   # 2 {'a1': 'a1', 'a2': 'a2'} ['a', 'a']

As no value is passed, the decorator set the default value for "dict1" and "list1" an empty dictionary and empty list

from default_mutable.DefaultMutable import defaultMutable

@defaultMutable('dict1', 'list1')
def function4(idx, dict1 = {}, list1 = [], element = 'a'):
    list1.append(element)
    dict1[f'{element}{idx}'] = f'{element}{idx}'
    print('', idx, dict1, list1)

print('')
print('function4:')
function4(idx= 1)   # 1 {'a1': 'a1'} ['a']
function4(idx= 2)   # 2 {'a2': 'a2'} ['a']

As no value is passed, the decorator set the default value for "dict1" and "list1" an empty dictionary and empty list

from default_mutable.DefaultMutable import defaultMutable

@defaultMutable('dict1', 'list1')
def function4_2(idx, dict1 = {}, list1 = ['init'], element = 'a'):
    list1.append(element)
    dict1[f'{element}{idx}'] = f'{element}{idx}'
    print('', idx, dict1, list1)

print('')
print('function4_2:')
function4_2(idx= 1)   # 1 {'a1': 'a1'} ['a']
function4_2(idx= 2)   # 2 {'a2': 'a2'} ['a']

As no value is passed, the decorator set the default value for "list1" an empty list. "dict1" has the default behavior

from default_mutable.DefaultMutable import defaultMutable

@defaultMutable('list1')
def function4_3(idx, dict1 = {}, list1 = [], element = 'a'):
    list1.append(element)
    dict1[f'{element}{idx}'] = f'{element}{idx}'
    print('', idx, dict1, list1)

print('')
print('function4_3:')
function4_3(idx= 1) # 1 {'a1': 'a1'} ['a']
function4_3(idx= 2) # 2 {'a1': 'a1', 'a2': 'a2'} ['a']

The default values are mutable and "list1" get incremented from one function to the other

def function5(idx, dict1 = {}, list1 = ['init'], element = 'a'):
    list1.append(element)
    dict1[f'{element}{idx}'] = f'{element}{idx}'
    print('', idx, dict1, list1)

print('')
print('function5:')
function5(idx= 1)   # 1 {'a1': 'a1'} ['init', 'a']
function5(idx= 2)   # 2 {'a1': 'a1', 'a2': 'a2'} ['init', 'a', 'a']

Default value for "list1" is not an empty list

from default_mutable.DefaultMutable import defaultMutable

@defaultMutable('dict1', list1= ['init'])
def function6(idx, dict1 = {}, list1 = ['init'], element = 'a'):
    list1.append(element)
    dict1[f'{element}{idx}'] = f'{element}{idx}'
    print('', idx, dict1, list1)

print('')
print('function6:')
function6(idx= 1)   # 1 {'a1': 'a1'} ['init', 'a']
function6(idx= 2)   # 2 {'a2': 'a2'} ['init', 'a']

Default value for "list1" is not an empty list

from default_mutable.DefaultMutable import defaultMutable

@defaultMutable('dict1', list1= ['init'])
def function7(idx, dict1 = {}, list1 = [], element = 'a'):
    list1.append(element)
    dict1[f'{element}{idx}'] = f'{element}{idx}'
    print('', idx, dict1, list1)

print('')
print('function7:')
function7(idx= 1)   # 1 {'a1': 'a1'} ['init', 'a']
function7(idx= 2)   # 2 {'a2': 'a2'} ['init', 'a']

Default value are empty dictionary and empty list

from default_mutable.DefaultMutable import defaultMutable

@defaultMutable('dict1', list1= [])
def function7_2(idx, dict1 = {}, list1 = ['init'], element = 'a'):
    list1.append(element)
    dict1[f'{element}{idx}'] = f'{element}{idx}'
    print('', idx, dict1, list1)

print('')
print('function7_2:')
function7_2(idx= 1) # 1 {'a1': 'a1'} ['a']
function7_2(idx= 2) # 2 {'a2': 'a2'} ['a']

Default value are empty dictionary and empty list

from default_mutable.DefaultMutable import defaultMutable

@defaultMutable('dict1', 'list1')
def function7_3(idx, dict1 = {}, list1 = ['init'], element = 'a'):
    list1.append(element)
    dict1[f'{element}{idx}'] = f'{element}{idx}'
    print('', idx, dict1, list1)

print('')
print('function7_3:')
function7_3(idx= 1) # 1 {'a1': 'a1'} ['a']
function7_3(idx= 2) # 2 {'a2': 'a2'} ['a']

Default value for "dict1" and "list1" are preset

from default_mutable.DefaultMutable import defaultMutable

@defaultMutable(dict1= {'dict_init': 'dict_init'}, list1= ['init'])
def function8(idx, dict1 = {}, list1 = [], element = 'a'):
    list1.append(element)
    dict1[f'{element}{idx}'] = f'{element}{idx}'
    print('', idx, dict1, list1)

print('')
print('function8:')
function8(idx= 1)   # 1 {'dict_init': 'dict_init', 'a1': 'a1'} ['init', 'a']
function8(idx= 2)   # 2 {'dict_init': 'dict_init', 'a2': 'a2'} ['init', 'a']

Passed values are not overridden

from default_mutable.DefaultMutable import defaultMutable

@defaultMutable(dict1= {'dict_init': 'dict_init'}, list1= ['init'])
def function9(idx, dict1 = {}, list1 = [], element = 'a'):
    list1.append(element)
    dict1[f'{element}{idx}'] = f'{element}{idx}'
    print('', idx, dict1, list1)

print('')
print('function9:')
function9(idx= 1, dict1= {'dict_value': 'dict_value'}, list1= ['list_value'])   # 1 {'dict_value': 'dict_value', 'a1': 'a1'} ['list_value', 'a']
function9(idx= 2, dict1= {'dict_value': 'dict_value'}, list1= ['list_value'])   # 2 {'dict_value': 'dict_value', 'a2': 'a2'} ['list_value', 'a']

Passed values are not overridden

from default_mutable.DefaultMutable import defaultMutable

@defaultMutable(dict1= {'dict_init': 'dict_init'}, list1= ['init'])
def function10(idx, dict1 = {}, list1 = [], element = 'a'):
    list1.append(element)
    dict1[f'{element}{idx}'] = f'{element}{idx}'
    print('', idx, dict1, list1)

print('')
print('function10:')
function10(1, {'dict_value': 'dict_value'}, ['list_value']) # 1 {'dict_value': 'dict_value', 'a1': 'a1'} ['list_value', 'a']
function10(2, {'dict_value': 'dict_value'}, ['list_value']) # 2 {'dict_value': 'dict_value', 'a2': 'a2'} ['list_value', 'a']

Metadata

Release files for default-mutable 0.1.0

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

Source distribution (sdist)

Source distribution for default-mutable 0.1.0
File Size Uploaded
default_mutable-0.1.0.tar.gz 4.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for default-mutable 0.1.0
File Interpreter ABI Platform
default_mutable-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 10.1 kB

Release files / default_mutable-0.1.0.tar.gz

Download URL default_mutable-0.1.0.tar.gz
Size 4.9 kB
Tags Source
SHA-256 checksum
How to use checksums
65a122fa1219efdc1cf2075468d32fb329ec2ab75271b48e5b91b3b1b5f24ea3
BLAKE2b-256 checksum
How to use checksums
15b8f697a35b5df1a73cfb0958c4788f26e51e8bc940ce3fe49840bac7e701cc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.13 CPython/3.8.0 Darwin/21.3.0

Release files / default_mutable-0.1.0-py3-none-any.whl

Download URL default_mutable-0.1.0-py3-none-any.whl
Size 5.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
578aef7579fb25a3f4beb5b3e1c0644c43cd18a6d4c446696eea1c82130c9403
BLAKE2b-256 checksum
How to use checksums
85cbebc03758753e537b7d7a929532eef558ed22452b5506c3322fb07fd06e62
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.13 CPython/3.8.0 Darwin/21.3.0

Release history Release notifications | RSS feed

This release

0.1.0 This release

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