Python函数中arg的默认值

时间:2017-03-14 07:11:56

标签: python

案例1:

async

输出:

def good_append(new_item, a_list=None):
    if a_list is None:
        a_list = []
    a_list.append(new_item)
    print a_list, id(a_list)
    return a_list

if __name__ == '__main__':
    one = good_append("one")
    two = good_append("two")

案例2:

['one'] 45267144
['two'] 45506312

输出:

def good_append(new_item, a_list=None):
    if a_list is None:
        a_list = []
    a_list.append(new_item)
    print a_list, id(a_list)

为什么在案例2中获得相同的['one'] 44611784 ['two'] 44611784

0 个答案:

没有答案