直接定义这样的函数时,Python会报错:
- Python 3.4.2 (v3.4.2:ab2c023a9432, Oct 6 2014, 22:16:31) [MSC v.1600 64 bit (AMD64)] on win32
- Type "copyright", "credits" or "license()" for more information.
- >>> def foo(p1, p2=6, p3):
- return 0
- SyntaxError: non-default argument follows default argument
- >>>
Python 3.4.2 (v3.4.2:ab2c023a9432, Oct 6 2014, 22:16:31) [MSC v.1600 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> def foo(p1, p2=6, p3):
return 0
SyntaxError: non-default argument follows default argument
>>>
因为调用函数时可能会产生歧义,比如调用上面的函数foo(1, 2),是该调用foo(1, 6, 2)呢?还是该调用foo(1, 6)呢?或者其它的什么呢?...
本文解释了Python中定义函数时常见的语法错误:非默认参数紧跟在默认参数之后导致的错误。通过具体示例展示了这种错误的原因及可能引起的歧义。
210

被折叠的 条评论
为什么被折叠?



