可爱的 Python:获得版本 2.0
2007-03-29 12:08:12 来源:WEB开发网核心提示: Python 程序员将熟悉前面用于定义函数定义中额外的位置和关键字自变量的语法,例如,可爱的 Python:获得版本 2.0(5),可以:定义额外的位置和关键字自变量>>>defmyfunc(this, that, *extras, **keywords):....pri
Python 程序员将熟悉前面用于定义函数定义中额外的位置和关键字自变量的语法。例如,可以:
定义额外的位置和关键字自变量
>>>
def
myfunc
(this, that, *extras, **keywords):
....
print
"Required arguments:", this, that
....
print
"Extra arguments:",
....
for
arg
in
extras:
print
arg,
....
print
"
Dictionary arguments:"
....
for
key,val
in
keywords.items():
print
"**", key,
"=", val
....
>>> myfunc(1)
Traceback (innermost last):
File
"<interactive input>", line 1,
in
?
TypeError:
not
enough arguments; expected 2, got 1
>>> myfunc(1,2)
Required arguments: 1 2
Extra arguments:
Dictionary arguments:
>>> myfunc(1,2,3,4,5)
Required arguments: 1 2
Extra arguments: 3 4 5
Dictionary arguments:
>>> myfunc(1,2,3, spam=17, eggs=32)
Required arguments: 1 2
Extra arguments: 3
Dictionary arguments:
** spam = 17
** eggs = 32
更多精彩
赞助商链接