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

3zz*_*zzy 1 python function

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

我有这个功能来剥离HTML标签,但它似乎只接受单个值,如果我想一次传递多个(非固定)值,我需要更改什么?

谢谢

bal*_*pha 8

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

def myfunc(*args):
    for argument in args:
        print "myfunc was given", argument
Run Code Online (Sandbox Code Playgroud)

如果您*args输入函数定义,则传递给函数的所有值都可以作为名为的元组使用args.请注意,您之前也可以添加其他参数*args,例如

def my_other_func(name, *args):
Run Code Online (Sandbox Code Playgroud)

所以第一个参数将可用name,其余的将在元组中args.

调用此参数是常规,但不是必需的args.只要前面有一个星号,你可以随意调用它.