The code is as follows:
Import Random, String
Class C (object): Pass
def dangerfunction (msg, l = [], B = {}, c = C ()):
Print msg, '-' *10
Print L, B, c.__dict__
L.append (1)
B[random.choice (string.ascii_lowercase)] = "
C.__dict__[random.choice (string.ascii_lowercase)] = ""
Print L, B, c.__dict__
Dangerfunction (' 1 ')
Dangerfunction (' 2 ')
Dangerfunction (' 3 ')
print '-' *20
def safefunction (msg, l = none, B = none, c = none):
If not l:l = []
If not b:b = {}
If not c:c = C ()
Print msg, '-' *10
Print L, B, c.__dict__
L.append (1)
B[random.choice (string.ascii_lowercase)] = "
C.__dict__[random.choice (string.ascii_lowercase)] = ""
Print L, B, c.__dict__
Safefunction (' 1 ')
Safefunction (' 2 ')
Safefunction (' 3 ')
Operation Result:
The code is as follows:
1----------
[] {} {}
[1] {' Q ': '} {' P ': '}
2----------
[1] {' Q ': '} {' P ': '}
[1, 1] {' Q ': ', ' A ': '} {' P ': ', ' G ': '}
3----------
[1, 1] {' Q ': ', ' A ': '} {' P ': ', ' G ': '}
[1, 1, 1] {' Q ': ', ' a ': ', ' W ': '} {' P ': ', ' W ': ', ' G ': '}
--------------------
1----------
[] {} {}
[1] {' K ': '} {' L ': '}
2----------
[] {} {}
[1] {' R ': '} {' C ': '}
3----------
[] {} {}
[1] {' Q ': '} {' H ': '}
The result of printing by dangerfunction is that the default value is [], (), class
The default value continues the last reference when the next call is continued with the default value if the argument is left empty.
May print without any logo can not see Clearly, plus the text should be much easier.
The code is as follows:
#-*-Coding:utf-8-*-
Import Random, String
Class C (object): Pass
def dangerfunction (msg, l = [], B = {}, c = C ()):
Print msg, '-' *10
Print U ' before Operation ', L, B, c.__dict__
L.append (1)
B[random.choice (string.ascii_lowercase)] = "
C.__dict__[random.choice (string.ascii_lowercase)] = ""
Print u ' after Operation ', L, B, c.__dict__
Dangerfunction (' 1 ')
Dangerfunction (' 2 ')
Dangerfunction (' 3 ')
print '-' * ten, U ' I am the delimiter ', '-' * 10
def safefunction (msg, l = none, B = none, c = none):
If not l:l = []
If not b:b = {}
If not c:c = C ()
Print msg, '-' *10
Print U ' before Operation ', L, B, c.__dict__
L.append (1)
B[random.choice (string.ascii_lowercase)] = "
C.__dict__[random.choice (string.ascii_lowercase)] = ""
Print u ' after Operation ', L, B, c.__dict__
Safefunction (' 1 ')
Safefunction (' 2 ')
Safefunction (' 3 ')
The code is as follows:
1----------
Before operation [] {} {}
After operation [1] {' m ': '} {' V ': '}
2----------
Before operation [1] {' m ': '} {' V ': '}
After operation [1, 1] {' I ': ', ' m ': '} {' G ': ', ' V ': '}
3----------
Before operation [1, 1] {' I ': ', ' m ': '} {' G ': ', ' V ': '}
After operation [1, 1, 1] {' I ': ', ' s ': ', ' m ': '} {' s ': ', ' g ': ', ' V ': '}
----------I'm a delimiter----------
1----------
Before operation [] {} {}
After operation [1] {' E ': '} {' Q ': '}
2----------
Before operation [] {} {}
After operation [1] {' d ': '} {' s ': '}
3----------
Before operation [] {} {}
After operation [1] {' m ': '} {' K ': '}