Why does (()) equal ()?
>>> (()) == ()
True
>>&g开发者_如何学JAVAt; (())
()
()
is a 0-tuple. (foo)
results in the value of foo
. Hence, (())
results in a 0-tuple.
From the tutorial:
; a tuple with one item is constructed by following a value with a comma (it is not sufficient to enclose a single value in parentheses).
For the same reason that (4) == 4
: adding parentheses around an expression does not alter its meaning (unless it would otherwise have been grouped differently of course).
Note that ( foo )
is not a 1-tuple. Otherwise things like 3 * (4 + 5)
would be an error as (4 + 5)
would be a 1-tuple containing 9 and you can't add a number to a 1-tuple.
I see now. From the tutorial.
; a tuple with one item is constructed by following a value with a comma (it is not sufficient to enclose a single value in parentheses).
So (()) is not the tuple that contains the empty tuple - this is that tuple: ((),)
精彩评论