[英]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.