简体   繁体   English

TypeError:__init __()接受4个位置参数,但给出了5个

[英]TypeError: __init__() takes 4 positional arguments but 5 were given

I have looked up similar questions, yet most problems are related to omitting the self argument in the __init__ definition. 我查找了类似的问题,但是大多数问题都与省略__init__定义中的self参数有关。

Code: 码:

class steamurl():

    baseurl = "http://api.steampowered.com/{0}/{1}/{2}/"

    def __init__(self, loc1, loc2, vnum, **options):
        self.loc1 = loc1
        self.loc2 = loc2
        self.vnum = vnum
        self.options = options

optionsdic = {
    'key': 'KEYHERE',
    'game_mode': 'all_pick',
    'min_players': '7'
    }

testurl = steamurl("IDOTA2Match_570", "GetMatchHistory", "v001", optionsdic)

However here my code was working fine before I added the "optionsdic" to the class. 但是,在我向类添加“ optionsdic”之前,我的代码运行良好。 After adding it I get the type error in the title. 添加后,标题中出现类型错误。 Am I using **kwargs incorrectly as an argument? 我是否错误地使用**kwargs作为参数?

You need to use ** to apply optionsdic as keyword arguments: 您需要使用**来将optionsdic用作关键字参数:

testurl = steamurl("IDOTA2Match_570", "GetMatchHistory", "v001", **optionsdic)

otherwise it is just another positional argument passing in a dictionary object. 否则,它只是传入字典对象的另一个位置参数。

This mirrors the syntax in the function signature. 这反映了函数签名中的语法。

如果你想传递的内容optionsdic作为单独的关键字参数,你需要使用** 拆包

testurl = steamurl("IDOTA2Match_570", "GetMatchHistory", "v001", **optionsdic)

You should call using ** : 您应该使用**致电:

testurl = steamurl("IDOTA2Match_570", "GetMatchHistory", "v001", optionsdic)

This will unpack the dictionary into separate keyword arguments. 这会将字典分解成单独的关键字参数。 In __init__ the keyword arguments will then be packed into a dictionary due to **options . __init__ ,由于**options ,关键字参数将被打包到字典中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError: __init__() takes from 1 to 3 positional arguments but 4 were given - TypeError: __init__() takes from 1 to 3 positional arguments but 4 were given python super:TypeError:__init __()接受2个位置参数,但给出了3个 - python super :TypeError: __init__() takes 2 positional arguments but 3 were given Money 和 TypeError:__init__() 需要 1 到 2 个位置参数,但给出了 3 个 - Money and TypeError: __init__() takes from 1 to 2 positional arguments but 3 were given 类型错误:__init__() 需要 2 个位置参数,但给出了 4 个 - TypeError: __init__() takes 2 positional arguments but 4 were given 多个 inheritance,TypeError: __init__() 需要 2 个位置 arguments 但给出了 3 个 - multiple inheritance, TypeError: __init__() takes 2 positional arguments but 3 were given 类型错误:__init__() 需要 3 个位置参数,但给出了 4 个 - TypeError: __init__() takes 3 positional arguments but 4 were given TypeError: __init__() 接受 2 个位置参数,但在 RFE 中给出了 3 个 - TypeError: __init__() takes 2 positional arguments but 3 were given in RFE 继承 TypeError: __init__() 接受 1 到 2 个位置参数,但给出了 8 个 - inheritance TypeError: __init__() takes from 1 to 2 positional arguments but 8 were given TypeError: __init__() 需要 2 个位置 arguments 但在 Selenium POP 中给出了 3 个 - TypeError: __init__() takes 2 positional arguments but 3 were given in Selenium POP TypeError:__init __()从1到2个位置参数,但给出了3个 - TypeError: __init__() takes from 1 to 2 positional arguments but 3 were given
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM