Skip to content

Instantly share code, notes, and snippets.

@DamonHao
Created May 2, 2015 06:58
Show Gist options
  • Select an option

  • Save DamonHao/68ab5fba9d209a80a0c4 to your computer and use it in GitHub Desktop.

Select an option

Save DamonHao/68ab5fba9d209a80a0c4 to your computer and use it in GitHub Desktop.
python default parameters
""" 在python里,默认参数值都是由函数进行引用的,所以前三个和最后一个函数调用使用的都是同一个默认的参数值"""
def foo(item, items=[], added=True):
if added:
items.append(item)
print items
foo(1)
foo(2)
foo(3, added=False)
foo(4, [])
foo(5)
"""
[1]
[1, 2]
[1, 2]
[4]
[1, 2, 5]
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment