Python: "Multiple" 函数中有多个参数
Python: "Multiple" multiple arguments in function
我是 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
?
不,你不能,因为你无法区分一种元素在何处停止,而另一种元素在何处开始。
让您的第一个参数接受单个字符串或序列,而不是:
def find(words, *sources):
if isinstance(words, str):
words = [words] # make it a list
# Treat words as a sequence in the rest of the function
现在您可以将其命名为:
find("string", source1, source2)
或
find(("string1", "string2"), source1, source2)
通过显式传入一个序列,您可以将它与多个来源区分开来,因为它本质上只是一个参数。
需要 "multiple multiple sources" 的通常解决方案是让 *args
成为第一个倍数,第二个倍数是元组。
>>> def search(target, *sources):
for i, source in enumerate(sources):
if target in source:
print('Found %r in %r' % (i, source))
return
print('Did not find %r' % target)
您会发现 Python 核心语言中使用的这种 API 设计的其他示例:
>>> help(str.endswith)
Help on method_descriptor:
endswith(...)
S.endswith(suffix[, start[, end]]) -> bool
Return True if S ends with the specified suffix, False otherwise.
With optional start, test S beginning at that position.
With optional end, stop comparing S at that position.
suffix can also be a tuple of strings to try.
>>> 'index.html'.endswith(('.xml', '.html', '.php'), 2)
True
>>> search(10, (5, 7, 9), (6, 11, 15), (8, 10, 14), (13, 15, 17))
Found 2 in (8, 10, 14)
请注意,后缀 可以是 tuple of strings to try
。
我是 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
?
不,你不能,因为你无法区分一种元素在何处停止,而另一种元素在何处开始。
让您的第一个参数接受单个字符串或序列,而不是:
def find(words, *sources):
if isinstance(words, str):
words = [words] # make it a list
# Treat words as a sequence in the rest of the function
现在您可以将其命名为:
find("string", source1, source2)
或
find(("string1", "string2"), source1, source2)
通过显式传入一个序列,您可以将它与多个来源区分开来,因为它本质上只是一个参数。
需要 "multiple multiple sources" 的通常解决方案是让 *args
成为第一个倍数,第二个倍数是元组。
>>> def search(target, *sources):
for i, source in enumerate(sources):
if target in source:
print('Found %r in %r' % (i, source))
return
print('Did not find %r' % target)
您会发现 Python 核心语言中使用的这种 API 设计的其他示例:
>>> help(str.endswith)
Help on method_descriptor:
endswith(...)
S.endswith(suffix[, start[, end]]) -> bool
Return True if S ends with the specified suffix, False otherwise.
With optional start, test S beginning at that position.
With optional end, stop comparing S at that position.
suffix can also be a tuple of strings to try.
>>> 'index.html'.endswith(('.xml', '.html', '.php'), 2)
True
>>> search(10, (5, 7, 9), (6, 11, 15), (8, 10, 14), (13, 15, 17))
Found 2 in (8, 10, 14)
请注意,后缀 可以是 tuple of strings to try
。