Created
May 2, 2015 06:58
-
-
Save DamonHao/68ab5fba9d209a80a0c4 to your computer and use it in GitHub Desktop.
python default parameters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ 在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