Python function default values are Shared with reference study notes

  • 2020-04-02 09:44:20
  • OfStack


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 results:

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': ''}

From the result of dangerFunction printing, the default value is [], (), class
The next time you make a call, if you continue with an empty parameter and use the default value, the default value continues the previous reference.

It may be hard to read without any logo, but it should be much easier to add text.


# -*- coding: utf-8 -*-
import random, string
class C(object):    pass
def dangerFunction(msg, l = [], b = {}, c = C()):
    print msg, '-'*10
    print u' Before operating ', l, b, c.__dict__
    l.append(1)
    b[random.choice(string.ascii_lowercase)] = ''
    c.__dict__[random.choice(string.ascii_lowercase)] = ""
    print u' After the operation ', l, b, c.__dict__
dangerFunction('1')
dangerFunction('2')
dangerFunction('3')
print '-' * 10, u' I'm the separator ', '-' * 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 operating ', l, b, c.__dict__
    l.append(1)
    b[random.choice(string.ascii_lowercase)] = ''
    c.__dict__[random.choice(string.ascii_lowercase)] = ""
    print u' After the operation ',l, b, c.__dict__
safeFunction('1')
safeFunction('2')
safeFunction('3')


1 ----------
 Before operating  [] {} {}
 After the operation  [1] {'m': ''} {'v': ''}
2 ----------
 Before operating  [1] {'m': ''} {'v': ''}
 After the operation  [1, 1] {'i': '', 'm': ''} {'g': '', 'v': ''}
3 ----------
 Before operating  [1, 1] {'i': '', 'm': ''} {'g': '', 'v': ''}
 After the operation  [1, 1, 1] {'i': '', 's': '', 'm': ''} {'s': '', 'g': '', 'v': ''}
----------  I'm the separator  ----------
1 ----------
 Before operating  [] {} {}
 After the operation  [1] {'e': ''} {'q': ''}
2 ----------
 Before operating  [] {} {}
 After the operation  [1] {'d': ''} {'s': ''}
3 ----------
 Before operating  [] {} {}
 After the operation  [1] {'m': ''} {'k': ''}


Related articles: