-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutable_default_parameter.py
57 lines (47 loc) · 1.14 KB
/
mutable_default_parameter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
'''
Case of mutable default function parameter
'''
def mutbl(test=[2, 3]):
print(test)
test.append('5')
print(test)
def mutblNone(test=None):
'''
Will raise exception if deafult is not explicitly
added like below
'''
if not test:
test = [1, 2]
print(test)
test.append('5')
print(test)
def nonmutbl(test=2222):
print(test)
test = 4444
print(test)
if __name__ == '__main__':
print('-----Mutable-----')
print('*** With value ***')
mutbl(test=[10, 11])
print('@@@ second call @@@')
mutbl(test=[10, 11])
print('*** Without value ***')
mutbl()
print('@@@ second call @@@')
mutbl()
print('Solution to the issue is setting default value immutable i.e., None')
print('*** With value ***')
mutblNone(test=[10, 11])
print('@@@ second call @@@')
mutblNone(test=[10, 11])
print('*** Without value ***')
mutblNone()
print('@@@ second call @@@')
mutblNone()
print('-----Non-Mutable-----')
print('*** With value ***')
nonmutbl()
print('*** Without value ***')
nonmutbl()
print('@@@ second call @@@')
nonmutbl()