gpt4 book ai didi

python - 将多个值传递给函数调用 (Python)

转载 作者:太空宇宙 更新时间:2023-11-04 06:51:24 25 4
gpt4 key购买 nike

import re
def strip_tags(value):
"Return the given HTML with all tags stripped."
return re.sub(r'<[^>]*?>', '', value)

我有剥离HTML标签的功能,但是好像只接受单个值,如果我想一次传递多个(不固定)值,我需要更改什么?

谢谢

最佳答案

Python 提供了一种使用任意长度参数列表的方法:

def myfunc(*args):
for argument in args:
print "myfunc was given", argument

如果您将 *args 放入您的函数定义中,所有传递给该函数的值都可以作为一个名为 args 的元组使用。请注意,您还可以在 *args 之前放置其他参数,喜欢

def my_other_func(name, *args):

因此第一个参数将作为 name 提供,其余参数将在元组 args 中。

将此参数称为 args 是惯例,但不是必需的。只要前面有一个星号,你就可以给它起任何你喜欢的名字。

关于python - 将多个值传递给函数调用 (Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2081929/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com