I have a function, which puts data into a database, called new_item()
:
我有一个函数,它将数据放入一个名为new_item()的数据库中:
def new_item(self, item, **optional):
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()
):
发送Web表单后,函数应该检查用户输入,然后使用此函数将用户输入放入数据库(我使用的是Flask,函数名是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})
But I get this error:
但我得到这个错误:
File X, line 99, in add_item
'ono': ono})
TypeError: new_item() takes exactly 2 arguments (3 given)
Fur debugging I print this statement right before I call the function add_item
. Console output is:
毛发调试我在调用函数add_item之前打印此语句。控制台输出是:
([u'iPhone 5', '791465667539154', u'2'],
{'price': u'99', 'place': u'Bossental', 'ono': True, 'desc': u'My brand new iPhone'})
I really don't know what's wrong. I never worked with **kwargs
before; is that related to the problem?
我真的不知道出了什么问题。我之前从未与** kwargs合作过;这与问题有关吗?
3 个解决方案
#1
12
You are providing three arguments to the function:
您正在为函数提供三个参数:
- The implicit
self
argument,Market
; - 隐含的自我论证,市场;
- The list, which will be
item
; and - 列表,将是项目;和
- The dictionary, which causes the problem.
- 字典,导致问题。
**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
).
** optional是一个特殊参数,它将所有尚未指定的关键字参数打包到字典中,在函数中可以作为可选参数(按照惯例,通常称为kwargs)。
A quick demonstration:
快速演示:
>>> 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'}
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 })
#2
2
Market.new_item(
[request.form['title'], session.get('user_id'), request.form['category']],
optional={
'desc': request.form['desc'],
'place': request.form['place'],
'price': request.form['price'],
'ono': ono
}
)
#3
2
If you want to pass keyword arguments then you need to specify the argument name while calling the function.
如果要传递关键字参数,则需要在调用函数时指定参数名称。
This link has more info on keyword args
此链接有关于关键字args的更多信息
May be you need to modify your code to the following. Then it would work
您可能需要将代码修改为以下内容。然后它会工作
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)
Hope this helps!
希望这可以帮助!
#1
12
You are providing three arguments to the function:
您正在为函数提供三个参数:
- The implicit
self
argument,Market
; - 隐含的自我论证,市场;
- The list, which will be
item
; and - 列表,将是项目;和
- The dictionary, which causes the problem.
- 字典,导致问题。
**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
).
** optional是一个特殊参数,它将所有尚未指定的关键字参数打包到字典中,在函数中可以作为可选参数(按照惯例,通常称为kwargs)。
A quick demonstration:
快速演示:
>>> 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'}
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 })
#2
2
Market.new_item(
[request.form['title'], session.get('user_id'), request.form['category']],
optional={
'desc': request.form['desc'],
'place': request.form['place'],
'price': request.form['price'],
'ono': ono
}
)
#3
2
If you want to pass keyword arguments then you need to specify the argument name while calling the function.
如果要传递关键字参数,则需要在调用函数时指定参数名称。
This link has more info on keyword args
此链接有关于关键字args的更多信息
May be you need to modify your code to the following. Then it would work
您可能需要将代码修改为以下内容。然后它会工作
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)
Hope this helps!
希望这可以帮助!