You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法将列表用作位置参数的技术问题求助

问题解决:列表作为位置参数传入类构造函数报错

你的代码报错是因为Tester(contestants)把整个contestants列表当作单个参数传给了__init__方法,但这个方法明确要求3个独立的整数参数(first、second、third),所以会提示参数缺失。

要把列表中的元素逐个作为位置参数传入,需要使用Python的星号解包操作符*,修改后的代码如下:

class Tester():
    def __init__(self, first: int, second: int, third: int) -> None:
        self.first = first
        self.second = second
        self.third = third

contestants = [54, 56, 32]

print(Tester(*contestants))

这里的*contestants会把列表中的三个元素分别拆成first=54、second=56、third=32三个参数,完美匹配__init__的参数要求,代码就能正常运行了。

内容的提问来源于stack exchange,提问作者Brian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 03:14:59