[英]TypeError: “__init__() got multiple values for keyword argument 'name'”
Why am I getting the error in the title when I call this constructor? 为什么在调用此构造函数时标题出现错误?
I call User(**args)
with the following dictionary as args
: 我用以下字典将
User(**args)
称为args
:
{'name': u'Rose Perrone', 'ipAddress': '127.0.0.1', 'email': u'hi@gmail.com'}
This is the constructor: 这是构造函数:
def __init__(name,
ipAddress,
password=None,
email=None,
deleted=None,
includePromoted=None,
explicit=None):
Firstly, make your first parameter self
. 首先,将第一个参数
self
。 You don't have to, but it is very, very common, and when someone is reading your code (like right now), it's annoying :p. 您不必这样做,但这是非常非常普遍的事情,当有人正在阅读您的代码(如现在)时,这很烦人:p。
name
is acting like self
here. name
在这里像self
一样。 You aren't supposed to pass a value for name
(self), because python already does that. 您不应该传递
name
(self)的值,因为python已经做到了。 However, you are doing this, because you have a key name
in your dictionary. 但是,您这样做是因为您的词典中有一个键
name
。 Thus, you're passing two things to one parameter, hence the error. 因此,您要将两件事传递给一个参数,从而导致错误。
To fix this: 要解决此问题:
Firstly, self
!!!! 首先,
self
!
But also don't forget to add in your name
parameter. 但也不要忘记添加您的
name
参数。
So altogether: 总共:
args = {'name': u'Rose Perrone', 'ipAddress': '127.0.0.1', 'email': u'hi@gmail.com'}
class MyClass:
def __init__(self,
name,
ipAddress,
password=None,
email=None,
deleted=None,
includePromoted=None,
explicit=None):
pass
MyClass(**args)
# <__main__.MyClass instance at blah>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.