gpt4 book ai didi

python - 函数声明中单独的星号 * 是什么意思?

转载 作者:太空狗 更新时间:2023-10-29 21:31:09 25 4
gpt4 key购买 nike

<分区>

以下代码中的* 是什么意思(在pprint 库中找到)?

def pformat(object, indent=1, width=80, depth=None, *, compact=False):
"""Format a Python object into a pretty-printed representation."""
return PrettyPrinter(indent=indent, width=width, depth=depth,
compact=compact).pformat(object)

如果它是 *args 那么它将是任意数量的位置参数。参数值将在名为 args 的元组中。前 4 个参数可以按名称或位置分配,参数 compact 只能按名称分配...

嗯,不!因为它不符合 the documentation :

In a function call, keyword arguments must follow positional arguments.

那么,星号在其他命名参数之前和之前做了什么?它是如何使用的?或者如果不使用它为什么会在那里?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com