- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
这些函数定义中的*args
和**kwargs
是什么意思?
def foo(x, y, *args):
pass
def bar(x, y, **kwargs):
pass
<小时/>
参见What do ** (double star/asterisk) and * (star/asterisk) mean in a function call?关于参数的补充问题。
最佳答案
*args
和 **kwargs
是一种常见的习惯用法,允许函数使用任意数量的参数,如 more on defining functions 部分所述。在 Python 文档中。
*args
将为您提供所有函数参数 as a tuple :
def foo(*args):
for a in args:
print(a)
foo(1)
# 1
foo(1,2,3)
# 1
# 2
# 3
**kwargs
将为您提供一切关键字参数,除了那些对应于作为字典的形式参数的参数。
def bar(**kwargs):
for a in kwargs:
print(a, kwargs[a])
bar(name='one', age=27)
# name one
# age 27
这两种习惯用法都可以与普通参数混合,以允许一组固定参数和一些可变参数:
def foo(kind, *args, **kwargs):
pass
也可以反过来使用:
def foo(a, b, c):
print(a, b, c)
obj = {'b':10, 'c':'lee'}
foo(100,**obj)
# 100 10 lee
*l
习语的另一种用法是在调用函数时解压参数列表。
def foo(bar, lee):
print(bar, lee)
l = [1,2]
foo(*l)
# 1 2
在 Python 3 中,可以在赋值的左侧使用 *l
( Extended Iterable Unpacking ),尽管在这种情况下它给出的是列表而不是元组:
first, *rest = [1,2,3,4]
first, *l, last = [1,2,3,4]
Python 3 还添加了新的语义(请参阅 PEP 3102 ):
def func(arg1, arg2, arg3, *, kwarg1, kwarg2):
pass
例如,以下内容在 python 3 中有效,但在 python 2 中无效:
>>> x = [1, 2]
>>> [*x]
[1, 2]
>>> [*x, 3, 4]
[1, 2, 3, 4]
>>> x = {1:1, 2:2}
>>> x
{1: 1, 2: 2}
>>> {**x, 3:3, 4:4}
{1: 1, 2: 2, 3: 3, 4: 4}
此类函数仅接受 3 个位置参数,并且 *
之后的所有内容只能作为关键字参数传递。
dict
,语义上用于关键字参数传递,是任意排序的。但是,在 Python 3.6 中,保证关键字参数记住插入顺序。**kwargs
中的元素顺序现在对应于关键字参数传递给函数的顺序。” -What’s New In Python 3.6 关于python - **(双星/星号)和*(星/星号)对参数有何作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53028374/
*args 和 **kwargs 是什么意思? def foo(x, y, *args): def bar(x, y, **kwargs): 最佳答案 *args 和 **kwargs 是一个常见的惯
*args 和 **kwargs 是什么意思? def foo(x, y, *args): def bar(x, y, **kwargs): 最佳答案 *args 和 **kwargs 是一个常见的惯
*args 和 **kwargs 是什么意思? def foo(x, y, *args):def bar(x, y, **kwargs): 最佳答案
在python文档中2.4.3. Formatted string literals ,似乎可以在 f 字符串的 {} 中写一个星号后跟一个表达式,但我找不到如何使用它。 那是什么以及如何使用它?它是
这些函数定义中的*args和**kwargs是什么意思? def foo(x, y, *args): pass def bar(x, y, **kwargs): pass 参见Wha
这些函数定义中的*args和**kwargs是什么意思? def foo(x, y, *args): pass def bar(x, y, **kwargs): pass 参见Wha
这个问题在这里已经有了答案: Delegated yield (yield star, yield *) in generator functions (3 个答案) 关闭 6 年前。 假设我创建了
我有时注意到使用 *偶尔搜索光标下的单词会有略微不同的行为(通常是当我在不同的计算机之间切换时)。问题是当我搜索前面有 * 的单词时(如 c++ 指针)。例如: MyPointer *foo; ...
This question already has answers here: Keyword only parameter (1个答案) Python documentation for os.re
这是我的数据: dput(dt) structure(c(15236000, 0, 0, 0, 0, 36722900, 45971100, 0, 0, 0, 0, 99067
所以我有一个看起来像这样但有 6k 行的数据框: AWC, LocationID 333, *Yukon 485, *Lewis Rich 76, *Kodiak 666, Kodiak 54, *R
我有一个简单的问题。我想列出我们可以使用关键字星号(或星号)* 的所有场景。 我只知道这些场景: Select * from Customers; Select Count(*) from Custo
瑞安·贝茨的Railscast about git ,他的 .gitignore 文件包含以下行: tmp/**/* 使用双星号后跟一个星号的目的是什么:**/*?简单地使用 tmp/* 而不是 tm
我想以这种方式选择带有 class="x"的元素的所有后代元素: .x * { color: red; } a b
这个问题在这里已经有了答案: What does ** (double star/asterisk) and * (star/asterisk) do for parameters? (25 个答案
最近我在 Programming Paradigms 上看到了这个视频和教授。使用星号、星形和符号等术语。 这就是他如何使用这些运算符的: int i = 37; float f = *(float*
我尝试在自定义按钮中居中对齐符号星号 (*),但我做不到。 如何像其他字符一样垂直居中对齐(1,2...)? 最佳答案 只需使用不同的字符。除了 * (ASTERISK U+002A),还有许多其他类
结论 概括的来说,就是对修饰的变量进行拆分, 对修饰的形式参数进行参数聚集。 单*号,将被修饰的变量按元素方式拆分, 对修饰的形式参数进行参数聚集。 双**号,将被修饰的变量按键值对进行拆分, 对
如果我想保持功能,并且不想在中间使用 *,那么等效的替代函数是什么?例如, import operator as op print(op.eq(*map(str.upper, ['a', 'A']))
我窥视 ng2-dropdown 我可以看到(ng2-dropdown-menu.ts) 除其他事项外 transition('hidden => visible', [
我是一名优秀的程序员,十分优秀!