Py学习  »  Python

Python 2中的可变长度optionals参数(*args)导致错误

Arvinth • 5 年前 • 1683 次点击  

我试图在Python2中运行下面的代码,但出现无效语法错误。

    columns = ["col1"]
    funcs = val_to_list(funcs)
    exprs = []

    for col_name in columns:
        for func in funcs:
            exprs.append((func, (col_name, *args)))

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/56098
文章 [ 1 ]  |  最新文章 5 年前
Martijn Pieters
Reply   •   1 楼
Martijn Pieters    5 年前

(col_name, *args) 使用创建新元组 col_name 作为第一个元素,然后是 args iterable unpacking 而且是 first added to Python 3.5

只需通过连接创建元组:

t =  (col_name,) + args  # assuming args is a tuple too
exprs.append((func, t))

如果 参数

t =  (col_name,) + tuple(args)  # works with any iterable.
exprs.append((func, t))