Prevent mutable default arguments behavior
Project description
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']
Project details
Release history Release notifications | RSS feed
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
File details
Details for the file default_mutable-0.1.0.tar.gz
.
File metadata
- Download URL: default_mutable-0.1.0.tar.gz
- Upload date:
- Size: 4.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.8.0 Darwin/21.3.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 65a122fa1219efdc1cf2075468d32fb329ec2ab75271b48e5b91b3b1b5f24ea3 |
|
MD5 | b4efb524e711fd9ea0d8c3647c1a9893 |
|
BLAKE2b-256 | 15b8f697a35b5df1a73cfb0958c4788f26e51e8bc940ce3fe49840bac7e701cc |
File details
Details for the file default_mutable-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: default_mutable-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.8.0 Darwin/21.3.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 578aef7579fb25a3f4beb5b3e1c0644c43cd18a6d4c446696eea1c82130c9403 |
|
MD5 | 84cc86a2390b76e3bed1a47c94f6b03c |
|
BLAKE2b-256 | 85cbebc03758753e537b7d7a929532eef558ed22452b5506c3322fb07fd06e62 |