*arg的用法

xiaoxiao2021-02-28  106

*args 的用法

*args 和 **kwargs 主要用于函数定义。 你可以将不定数量的参数传递给一个函数。

这里的不定的意思是:预先并不知道, 函数使用者会传递多少个参数给你, 所以在这个场景下使用这两个关键字。 *args 是用来发送一个非键值对的可变数量的参数列表给一个函数.

这里有个例子帮你理解这个概念:

def test_var_args(f_arg, *argv): print("first normal arg:", f_arg) for arg in argv: print("another arg through *argv:", arg) test_var_args('yasoob', 'python', 'eggs', 'test')

这会产生如下输出:

first normal arg: yasoob another arg through *argv: python another arg through *argv: eggs another arg through *argv: test
转载请注明原文地址: https://www.6miu.com/read-44381.html

最新回复(0)