[英]How to pass through Python args and kwargs?
While I have a general understanding (I think) of Python's *args and **kwargs, I'm having trouble understanding how to pass them from one function through to another.虽然我对 Python 的 *args 和 **kwargs 有一个大致的了解(我认为),但我无法理解如何将它们从一个函数传递到另一个函数。 Here's my model:
这是我的模型:
from pdb import set_trace as debug
from django.db import models
class Person(models.Model):
name = models.CharField(max_length=30)
def __unicode__(self):
return u'%s' % self.name
def save_name_for(self, *args, **kwargs):
self.name = 'Alex'
return self
def save_name(self, *args, **kwargs):
debug()
self.save_name_for(self, args, kwargs)
self.save()
I've split saving a name into two functions above.我已将保存名称拆分为上面的两个函数。 This way I can unit-test the logic I would normally put all in the save_name method by unit-testing the save_name_for method instead.
通过这种方式,我可以通过对 save_name_for 方法进行单元测试来对通常将所有内容放在 save_name 方法中的逻辑进行单元测试。
When I run this in the interpreter and stop in the save_name method, as I would expect, I see this:当我在解释器中运行它并在 save_name 方法中停止时,正如我所料,我看到了:
(Pdb) args
self =
args = (1, 2)
kwargs = {'last': 'Doe', 'first': 'John'}
If I then step into the save_name_for method, I see this:如果我随后进入 save_name_for 方法,我会看到:
(Pdb) args
self =
args = (<Person: >, (1, 2), {'last': 'Doe', 'first': 'John'})
kwargs =
Is there some way to pass the kwargs that are received by the save_name method directly into save_name_for method so that they appear in the latter's kwargs?有没有办法将 save_name 方法接收到的 kwargs 直接传递给 save_name_for 方法,以便它们出现在后者的 kwargs 中? I'd like to see something like this in save_name_for method's name space:
我想在 save_name_for 方法的名称空间中看到类似的内容:
(Pdb) args
self =
args = (1, 2)
kwargs = {'last': 'Doe', 'first': 'John'} # <= I want this
I realize I could parse them in save_name and then pass them explicitly to save_name_for but that seems rather inelegant.我意识到我可以在 save_name 中解析它们,然后将它们显式传递给 save_name_for ,但这似乎很不优雅。 I also thought I might do this since args is a tuple...
我还想我可能会这样做,因为 args 是一个元组......
kwargs = args[2]
... but it doesn't appear to work. ...但它似乎不起作用。 args[2] is just everything (I don't understand this).
args[2] 就是一切(我不明白这一点)。 Is there a Pythonic way to do this?
有没有一种 Pythonic 的方法来做到这一点?
The *
and **
operators are used in two different situations. *
和**
运算符用于两种不同的情况。
When used as part of a function definition ,当用作函数定义的一部分时,
def save_name_for(self, *args, **kwargs):
it is used to signify an arbitrary number of positional or keyword arguments, respectively.它用于分别表示任意数量的位置或关键字参数。 The point to remember is that inside the function
args
will be a tuple , and kwargs
will be a dict .要记住的一点是,函数内部
args
将是一个 tuple ,而kwargs
将是一个 dict 。
When used as part of a function call ,当用作函数调用的一部分时,
args = (1, 2) kwargs = {'last': 'Doe', 'first': 'John'} self.save_name_for(*args, **kwargs)
the *
and **
act as unpacking operators . *
和**
充当解包操作符。 args
must be an iterable , and kwargs
must be dict-like . args
必须是 iterable ,而kwargs
必须是 dict-like 。 The items in args
will be unpacked and sent to the function as positional arguments, and the key/value pairs in kwargs
will be sent to the function as keyword arguments. args
中的项目将被解包并作为位置参数发送给函数,而kwargs
中的键/值对将作为关键字参数发送给函数。 Thus,因此,
self.save_name_for(*args, **kwargs)
is equivalent to相当于
self.save_name_for(1, 2, last='Doe', first='John')
See also the saltycrane blog for an explanation with examples.另请参阅saltycrane 博客以获取示例说明。
You pass them with syntax mirroring the argument syntax:您使用与参数语法相同的语法传递它们:
self.save_name_for(*args, **kwargs)
Note that you do not need to pass in self
;请注意,您不需要传入
self
; save_name_for
is already bound. save_name_for
已经绑定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.