TypeError:function(self,item,** kwargs)恰好接受2个参数(给定3个) [英] TypeError: function(self, item, **kwargs) takes exactly 2 arguments (3 given)

查看:52
本文介绍了TypeError:function(self,item,** kwargs)恰好接受2个参数(给定3个)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数,该函数将数据放入数据库中,称为 new_item():

I have a function, which puts data into a database, called new_item():

def new_item(self, item, **optional):

发送Web表单后,函数应检查用户输入,然后使用此函数将用户输入放入数据库中(我使用的是Flask,函数名称为 add_item()):

After sending a web form, a function should check the user input and then use this function to put the user input into the database (I'm using Flask, function name is add_item()):

Market.new_item([request.form['title'], 
                 session.get('user_id'), 
                 request.form['category']], 
                {'desc': request.form['desc'], 
                 'place': request.form['place'], 
                 'price': request.form['price'], 
                 'ono': ono})

但是我得到这个错误:

File X, line 99, in add_item
'ono': ono})
TypeError: new_item() takes exactly 2 arguments (3 given)

Fur调试在调用函数 add_item 之前,我立即打印此语句.控制台输出为:

Fur debugging I print this statement right before I call the function add_item. Console output is:

([u'iPhone 5', '791465667539154', u'2'], 
 {'price': u'99', 'place': u'Bossental', 'ono': True, 'desc': u'My brand new iPhone'})

我真的不知道怎么了.我以前从未使用过 ** kwargs ;与问题有关吗?

I really don't know what's wrong. I never worked with **kwargs before; is that related to the problem?

推荐答案

您正在为函数提供三个参数:

You are providing three arguments to the function:

  1. 隐式 self 自变量 Market ;
  2. 列表,将是 item ;和
  3. 导致问题的字典.

** optional 是一个特殊参数,它将尚未指定的所有关键字参数打包到字典中,在函数中可以作为 optional 访问(按照惯例,通常称为 kwargs ).

**optional is a special argument, that packs all of the keyword arguments not already specified into a dictionary, accessible within the function as optional (by convention, this is usually called kwargs).

快速演示:

>>> def demo(x, y=None, **kwargs):
    print 'x: {0}'.format(x)
    print 'y: {0}'.format(y)
    print 'kwargs: {0}'.format(kwargs)


>>> demo('foo', y='bar', z='baz')
x: foo # 'x' positional argument
y: bar # 'y' keyword argument
kwargs: {'z': 'baz'} # unspecified keyword arguments

您也可以使用 ** 将字典分解成关键字参数:

You can unpack a dictionary into keyword arguments with ** too:

>>> demo('foo', **{'y': 'bar', 'z': 'baz'})
x: foo
y: bar
kwargs: {'z': 'baz'}

因此,如果要将字典的内容传递到 ** optional 参数中,则可以使用相同的语法将字典解压缩为关键字参数:

Therefore if you want to pass the contents of the dictionary into the **optional argument, you could use that same syntax to unpack the dictionary into keyword arguments:

Market.new_item([request.form['title'], 
                 session.get('user_id'), 
                 request.form['category']], 
                **{'desc': request.form['desc'],
              # ^ note asterisks
                   'place': request.form['place'],
                   'price': request.form['price'],
                   'ono': ono })

这篇关于TypeError:function(self,item,** kwargs)恰好接受2个参数(给定3个)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆