gpt4 book ai didi

python-3.x - 为什么python中的star解包会给出一个列表

转载 作者:行者123 更新时间:2023-12-01 02:43:09 24 4
gpt4 key购买 nike

我很好奇为什么 python3 的星形解包返回列表实例而不是元组(函数参数中由星号返回)。这是 python 的特质,还是背后有充分的理由。

In [1]: def foo(*args):
...: print(type(args))
...:

In [2]: foo(1, 2, 3, 4)
<class 'tuple'>

In [3]: first, *rest = (1, 2, 3, 4)

In [4]: type(rest)
Out[4]: list

最佳答案

They figured a list would be easier to process:

After a short discussion on the python-3000 list [1], the PEP was accepted by Guido in its current form. Possible changes discussed were:

  • ...
  • Make the starred target a tuple instead of a list. This would be consistent with a function's *args, but make further processing of the result harder.

关于python-3.x - 为什么python中的star解包会给出一个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48977480/

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