位置参数

来源:互联网 发布:淘宝德国进口净水器 编辑:程序博客网 时间:2024/05/16 14:38

位置参数必须在被调用函数中定义的准确顺序来传递。

>>> def foo(who):                                                     #defined for only 1 argument
    print("hello",who)

>>> foo()                                                                     #o argument ..BAD
Traceback (most recent call last):
  File "<pyshell#350>", line 1, in <module>
    foo()
TypeError: foo() missing 1 required positional argument: 'who'


>>> foo("word!")                                                         #1 argument ...WORKS
hello word!


>>> foo("MR.","Word!")                                              #2 arguments ...BAD
Traceback (most recent call last):
  File "<pyshell#352>", line 1, in <module>
    foo("MR.","Word!")
TypeError: foo() takes 1 positional argument but 2 were given


foo()函数有一个位置参数。意味着任何对foo()的调用必须有唯一的一个参数,不多不少。否则会频频看到TypeError。

原创粉丝点击