【发布时间】:2015-08-22 16:32:14
【问题描述】:
我是 Python 新手,但我知道我可以使用 *args 在一个函数中允许可变数量的多个参数。
此脚本在任意数量的字符串*sources 中查找word:
def find(word, *sources):
for i in list(sources):
if word in i:
return True
source1 = "This is a string"
source2 = "This is Wow!"
if find("string", source1, source2) is True:
print "Succeed"
但是,是否可以在一个函数中指定 "multiple" 多个参数 (*args)?在这种情况下,这将在多个 *sources 中寻找多个 *words。
形象地说:
if find("string", "Wow!", source1, source2) is True:
print "Succeed"
else:
print "Fail"
我怎样才能让脚本辨别什么是word,什么应该是source?
【问题讨论】:
-
如果您有多个参数,您将如何调用该函数? python如何知道将哪个值放入哪个?