Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does (()) equal ()?

Tags:

python

>>> (()) == ()
True
>>> (())
()
like image 568
ripper234 Avatar asked Mar 19 '11 10:03

ripper234


People also ask

What is equals () used for?

The equals() method compares two strings, and returns true if the strings are equal, and false if not.

Why do we use .equals instead of ==?

We can use == operators for reference comparison (address comparison) and . equals() method for content comparison. In simple words, == checks if both objects point to the same memory location whereas . equals() evaluates to the comparison of values in the objects.

What is the difference between == and equal ()?

The major difference between the == operator and . equals() method is that one is an operator, and the other is the method. Both these == operators and equals() are used to compare objects to mark equality.

What does === means?

The strict equality operator ( === ) checks whether its two operands are equal, returning a Boolean result. Unlike the equality operator, the strict equality operator always considers operands of different types to be different.


2 Answers

() 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).

like image 180
Ignacio Vazquez-Abrams Avatar answered Sep 22 '22 12:09

Ignacio Vazquez-Abrams


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.

like image 36
sepp2k Avatar answered Sep 23 '22 12:09

sepp2k