gpt4 book ai didi

python - 为什么元组用括号括起来?

转载 作者:太空狗 更新时间:2023-10-29 20:42:00 24 4
gpt4 key购买 nike

a tuple is a comma-separated list of values

所以声明元组的有效语法是:

tup = 'a', 'b', 'c', 'd'

但是我经常看到的是这样的声明:

tup = ('a', 'b', 'c', 'd')

将元组括在圆括号中有什么好处?

最佳答案

来自Python docs :

... so that nested tuples are interpreted correctly. Tuples may be input with or without surrounding parentheses, although often parentheses are necessary anyway (if the tuple is part of a larger expression).

嵌套元组的例子:

tuple = ('a', ('b', 'c'), 'd')

关于python - 为什么元组用括号括起来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35241105/

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