命名占位符、.format(**DictObject,…)是使用**kwargs的示例吗?

2024-05-26 07:45:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我在一个示例程序中发现了以下代码:

>>> monitor = {'top': 5, 'left': 0, 'width': 1000, 'height': 200}
>>> output = 'sct-{top}x{left}_{width}x{height}.png'.format(**monitor)
>>> print(output)
sct-5x0_1000x200.png

看起来.format(**monitor)是一种**kwargs类型的东西,但是我以前没有用过这些东西。你知道吗

我“解除”了这个代码,知道它是如何工作的,但我的理解能让我陷入困境吗?你知道吗

我正要发布这个问题,但先运行了这个:

>>> '{} {} buckle my {top}'.format(1, 2, **monitor)
'1 2 buckle my 5'

所以我想我现在可以说我积极地与kwargs合作。你知道吗


Tags: 代码程序format示例outputpngmytop