简体   繁体   English

TypeError:“ __ init __()为关键字参数'name'获得了多个值”

[英]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: 要解决此问题:

  1. Firstly, self !!!! 首先, self

  2. 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.

相关问题 TypeError:__init __()为关键字参数“ choices”获得了多个值 - TypeError: __init__() got multiple values for keyword argument 'choices' TypeError:__ init __()得到关键字参数&#39;customer&#39;的多个值 - TypeError: __init__() got multiple values for keyword argument 'customer' SQLAlchemy TypeError:__init __()为参数&#39;name&#39;获得了多个值 - SQLAlchemy TypeError: __init__() got multiple values for argument 'name' 类型错误:__init__() 得到了意外的关键字参数“名称”-CONVOKIT - TypeError: __init__() got an unexpected keyword argument 'name' - CONVOKIT TypeError at &#39;&#39; __init__() 得到一个意外的关键字参数 &#39;&#39; - TypeError at '' __init__() got an unexpected keyword argument '' 在Scrapy中使用规则和请求会引发异常TypeError:__init __()为关键字参数&#39;callback&#39;获得了多个值 - Using Rules and Requests in Scrapy throws exception TypeError: __init__() got multiple values for keyword argument 'callback' / __init __()的Django Rest Framework TypeError为关键字参数&#39;read_only&#39;获取了多个值 - Django Rest Framework TypeError at / __init__() got multiple values for keyword argument 'read_only' __init__() 为关键字参数“列”获得了多个值 - __init__() got multiple values for keyword argument 'columns' windrose:__init __()获得了多个关键字参数值 - windrose: __init__() got multiple values for keyword argument __init __()为关键字参数“ bar”获得了多个值 - __init__() got multiple values for keyword argument 'bar'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM