Python function default value share with reference learning notes

Source: Internet
Author: User
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 ': '}

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.