- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我刚遇到这种让我感到惊讶的行为:
def my_func(a=4, **kwargs):
print kwargs
演示:
>>> my_func(a=5, b=6)
{'b': 6} # I was expecting {'a' : 4, 'b' : 6}
# Maybe {'a' : 5, 'b' : 6}
此外,如果我得到:
语法错误:关键字参数重复
如在
>>> my_func(a=4, a=5)
File "<stdin>", line 1
SyntaxError: keyword argument repeated
或TypeError: my_func() got multiple values for keyword argument 'a'
如在
>>> my_func(a=4, **{'a' : 5, 'b' : 6})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: my_func() got multiple values for keyword argument 'a'
python 删除关键字'a'遵循什么规则?
也许我遗漏了一些明显的东西,或者一个关键的术语,但我无法通过搜索找到解决方案。
最佳答案
a
没有被删除,它只是没有被包含在 **kwargs
中,因为您在函数定义中明确定义了它。因此,如果我编辑您的原始示例:
def my_func(a=4, **kwargs):
print kwargs
print a
然后测试一下:
>>> my_func(a=5, b=7)
{'b': 7}
5
**kwargs
参数用于收集函数调用中给出的可选关键字参数,这些参数未明确包含在函数定义中。由于您在 my_func
的定义中包含了 a=4
,因此它不会包含在 **kwargs
中。
Python documentation 中提到了这一点(强调我的):
When a final formal parameter of the form **name is present, it receives a dictionary (see Mapping Types — dict) containing all keyword arguments except for those corresponding to a formal parameter.
关于python - 如果存在具有相同名称的位置参数,为什么 kwargs 中的键被删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23770682/
我有一个像 s = "title='bah' name='john and jill' purple='haze' none=None i=1" 我正在寻找一种将其放入字典的 Pythonic 方式(
我正在通过教程阅读有关模板 View 的内容,其中一些代码让我感到困惑。作者使用了这个代码示例 from django.utils.timezone import now class AboutUsV
看不懂下面的例子,假设我有这些功能: # python likes def save(filename, data, **kwargs): fo = openX(filename, "w",
假设我们有一个像这样的函数声明 def myfunc(a=None,b=None): as_dict = {"a": a, "b": b,
def a(**akwargs): def b(bkwargs=akwargs): # how to not only use akwargs defaultly,but al
阅读 Python 文档,有几种创建字典的方法。 dict() dict(**kwargs) dict(mapping, **kwargs) dict(iterable, **kwargs) http
两种方法我都见过,但我不明白它们的区别以及我应该将什么作为“最佳实践”: def custom_function(**kwargs): foo = kwargs.pop('foo')
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 8 年前。 Improve t
我需要将 args 和 kwargs 都存储在一个元组中以便稍后调用,那么在这种情况下元组中的适当值是 *args 还是 args?换句话说,这行得通吗: def __init__(self, *ar
我有这个查询 Location.objects.filter(locations_rate__rate=search_rate).distinct('id') 如何将 distinct() 设置为 *
是否可以简化 kwargs 选项的 bool 检查? 例如在 foo 中我必须检查很多选项: def foo(*args, **kwargs): if 'foo' in kwargs and k
我尝试运行 Python Data Science Essential 一书中的一个示例。但是,当我运行它时出现错误。实际上,我才刚刚开始学习 python。所以,我觉得很难修复这些错误。请帮我。这是
我正在尝试使用以下代码在一个大图中创建多个seaborn regplot: %matplotlib notebook import seaborn as sns from itertools impo
我有一个这样的函数:。如果有参数x,则它一定是布尔值。任何其他命名参数必须为int。。其他人将编写调用foo的函数,我希望他们能够传递x和kwargs。目前,这意味着调用foo()的每个函数除了kwa
当一个类继承自单个类时,调用父方法的首选方式是什么?我知道有两种调用父方法的方法。 选项 1: ParentClass.method(self, *args, **kwargs) 选项 2: supe
问题 我在 Dusty Phillips 的 Object Oriented Programming(为简洁起见而简化)中遇到了这段代码,但我不确定这个定义的特定部分。 class A: de
在创建数据类对象时我可以使用 kwargs 没有问题: @dataclass() class Data: name: str = 'Unnamed' additiona
在创建数据类对象时我可以使用 kwargs 没有问题: @dataclass() class Data: name: str = 'Unnamed' additiona
我在下面运行了一个装饰器演示。 def logger(func): def inner(*args, **kwargs): print(args) print(
如何使用“一个”类参数**kwargs设置对象属性? 我想要的是在一个循环中执行此代码: class purchase(): def __init__(self,**kwargs):
我是一名优秀的程序员,十分优秀!