gpt4 book ai didi

python - 当默认提供的不是元组时,为什么 python 中的链式字典 .get() 返回元组?

转载 作者:太空狗 更新时间:2023-10-29 22:04:16 28 4
gpt4 key购买 nike

Python 2.6.6 当我对 .get 的结果调用 .get 时,结果是一个元组。这对我来说毫无意义。示例:

box = {}.get('test1',{}).get('test2','hrmm'),
print type(box)

打印出来

<type 'tuple'>

这对我来说毫无意义。显然,第二个 get 中的默认值是一个简单的字符串。那么是什么给了?感谢您的任何见解。

最佳答案

行尾有一个逗号,因此您得到的是 {}.get('test1',{}).get('test2','hrmm') 在单元素元组中。

这是一个如何使用简单文字的示例:

>>> box = 1,
>>> box
(1,)
>>> type(box)
<type 'tuple'>

关于python - 当默认提供的不是元组时,为什么 python 中的链式字典 .get() 返回元组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13616455/

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