【问题标题】:Calling a method with named arguments causes “multiple values for argument” error使用命名参数调用方法会导致“参数的多个值”错误
【发布时间】:2021-04-10 05:58:20
【问题描述】:

我正在尝试从 Youtube Data API 获取我的 youtube 统计信息,然后将所有统计信息放在图像上。

当我尝试将参数传递给我的图像生成器函数时,

# vid_stats and c_stats are dictionaries, 
# but in them, there's no "video_title" key or anything
statimager.create_image(video_title="Video title", video_stats=vid_stats, channel_stats=c_stats)

它给了我这个类型错误:

Traceback (most recent call last):
  File "D:\_Prog\Python\ytstatupdater\ytstatupdater.py", line 118, in <module>
    statimager.create_image(video_title="Video title", video_stats=vid_stats, channel_stats=c_stats)
TypeError: create_image() got multiple values for argument 'video_title'

这里是 create_image() 函数的定义:

def create_image(video_title, video_stats, channel_stats):

在这个函数中,我只使用这些参数将video_title,以及video_statschannel_stats 中的字符串放入一个字符串数组中。

我真的不明白为什么程序要传递多个值作为 video_title,因为 video_title 是单个字符串对象。

我正在使用 Python 3.9.1。

【问题讨论】:

  • video_title 究竟包含什么?你能通过打印来验证它并向我们展示你得到了什么吗?
  • create_imagestatimager的静态方法还是statimager的实例方法?
  • 我认为你需要一个minimal reproducible example。仅凭这些部分很难了解问题的完整背景。
  • 您是否忘记将self 作为create_image() 方法的第一个参数?
  • 所有方法都需要有self作为第一个参数,这似乎是方法create_image中缺少的

标签: python python-imaging-library youtube-data-api


【解决方案1】:

所以在 cmets 中,@ted-klein-bergman 提到我忘记将 self 作为第一个参数放在定义中。
我已经把它放在定义中,它现在可以工作了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-11
    • 1970-01-01
    • 2022-09-23
    • 1970-01-01
    • 2012-12-10
    • 2020-10-22
    • 1970-01-01
    相关资源
    最近更新 更多