为什么元组使用括号()而不是像<>这样的东西? [英] Why tuples use parentheses ()'s instead of something else like <>'s?

查看:239
本文介绍了为什么元组使用括号()而不是像<>这样的东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每个人都知道元组是用括号()来定义的。


这会导致1项元组的混淆,因为(5)可以解释为

作为元组或数学表达式中的数字5

,例如x =(5)*(4 + 6)。


Wouldn为了避免这种混淆,用<>'或{}'s或

来定义元组是更好的选择?


或许()因为我不知道的其他原因是个好主意吗?


请赐教,因为我真的很想知道。

克里斯


PS我喜欢Python!

Tuples are defined with regards to parentheses ()''s as everyone knows.

This causes confusion for 1 item tuples since (5) can be interpreted
as a tuple OR as the number 5 in a mathematical expression
such as x = (5) * (4+6).

Wouldn''t it have been better to define tuples with <>''s or {}''s or
something else to avoid this confusion??

Perhaps ()''s are a good idea for some other reason I don''t know?

Please enlighten me as I really want to know.
Chris

P.S. I love Python!

推荐答案

se ****** @ spawar.navy.mil 写道:
se******@spawar.navy.mil wrote:
元组是关于圆括号()的定义,众所周知。

这会导致1项元组的混淆,因为(5)可以解释为元组或数学表达式中的数字5,例如x =(5)*(4 +6)。


不,(5)总是数字5.要制作单元素元组,请使用(5,)。

不是吗最好用<>'或{}'或其他东西定义元组以避免这种混乱?

也许()是好的我不知道其他原因的想法?
Tuples are defined with regards to parentheses ()''s as everyone knows.

This causes confusion for 1 item tuples since (5) can be interpreted
as a tuple OR as the number 5 in a mathematical expression
such as x = (5) * (4+6).
No, (5) is always the number 5. To make a one-element tuple, use (5,).
Wouldn''t it have been better to define tuples with <>''s or {}''s or
something else to avoid this confusion??

Perhaps ()''s are a good idea for some other reason I don''t know?




实际上,对于非空元组,括号并不是必需的,

除非代码含糊不清。



Actually, for non-empty tuples, the parentheses aren''t really necessary,
unless code is ambiguous.

x = 1,2,3
x
(1,2,3)y = 5,
y
(5,)


但是:

print 8 ,9#不是元组
8 9打印(8,9)
x = 1, 2, 3
x (1, 2, 3) y = 5,
y (5,)

but:
print 8, 9 # not a tuple 8 9 print (8, 9)



(8,9)

HTH,


-

Hans Nowak
http://zephyrfalcon.org/


(8, 9)

HTH,

--
Hans Nowak
http://zephyrfalcon.org/


se ****** @ spawar.navy.mil 写道:
定义不是更好带有<>'或{}'s的元组或
其他东西以避免这种混乱?
Wouldn''t it have been better to define tuples with <>''s or {}''s or
something else to avoid this confusion??




我看到的方式,元组只是一种函数返回的方式

一次多个值。当你以这种方式想到它们时,你甚至不需要b $ b需要括号:


def foo():

如果we_found_stuff:

返回200,''长而无聊的结果''

否则:

返回404,''找不到''


status_code,body = foo()

如果foo()只需要返回一个值,它会在正常情况下执行/>
方式,你不需要担心1元组。



The way I see it, tuples are just a way of having a function return
multiple values at once. When you think of them that way, you don''t even
need parenthesis:

def foo():
if we_found_stuff:
return 200, ''long and boring result''
else:
return 404, ''nothing found''

status_code, body = foo()

If foo() only needed to return one value, it would do so in the normal
way, and you wouldn''t need to worry about 1-tuples.


* se ****** @ spawar.navy.mil
* se******@spawar.navy.mil
也许()是个好主意还有其他原因我不知道?
Perhaps ()''s are a good idea for some other reason I don''t know?




单元素元组写成(4,)。


-

Marius Bernklev


< URL: http://www.ping.uio.no/~mariube/ >



One-element tuples are written as (4,).

--
Marius Bernklev

<URL: http://www.ping.uio.no/~mariube/ >


这篇关于为什么元组使用括号()而不是像&lt;&gt;这样的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆