社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

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

Arvinth • 4 年前 • 805 次点击  

我试图在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
 
805 次点击  
文章 [ 1 ]  |  最新文章 4 年前
Martijn Pieters
Reply   •   1 楼
Martijn Pieters    4 年前

(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))