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. After adding it I get the type error in the title. Am I using **kwargs
incorrectly as an argument?
然而,在我向类中添加“optionsdic”之前,我的代码工作正常。添加后,我在标题中得到类型错误。我错误地使用** kwargs作为参数吗?
3 个解决方案
#1
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.
这反映了函数签名中的语法。
#2
If you want to pass the contents of optionsdic
as separate keyword arguments, you need to use **
unpacking:
如果要将optionsdic的内容作为单独的关键字参数传递,则需要使用**解包:
testurl = steamurl("IDOTA2Match_570", "GetMatchHistory", "v001", **optionsdic)
#3
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__中,由于**选项,关键字参数将被打包到字典中。
#1
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.
这反映了函数签名中的语法。
#2
If you want to pass the contents of optionsdic
as separate keyword arguments, you need to use **
unpacking:
如果要将optionsdic的内容作为单独的关键字参数传递,则需要使用**解包:
testurl = steamurl("IDOTA2Match_570", "GetMatchHistory", "v001", **optionsdic)
#3
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__中,由于**选项,关键字参数将被打包到字典中。