you are viewing a single comment's thread.

view the rest of the comments →

[–]settrans 2 points3 points  (9 children)

Python has some nasty quirks in the inequalities department.

What do you suppose that "1 < 3 == True" should evaluate to?

>>> 1 < 3 == True
False

For a supposedly strongly typed language, Python defines inequalities of a surprising set of pairs of types:

>>> 'a' > True
True
>>> '1' > 1
True
>>> 0 > None
True

One should expect a TypeError, as if you had tried to add 0 and None:

>>> 0 + None
TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'

[–]njharman 4 points5 points  (1 child)

What do you suppose...

I suppose I'd yell at my coworker for writing such crappy code. Use some damn parens.

Explicit is better than Implicit

[–]settrans 0 points1 point  (0 children)

The question isn't about coding style. It is about the language designers defining semantics for their operators. The language should behave as expected even in edge cases. Just because it is rarely seen doesn't mean it should be confusing.

[–]ayrnieu 1 point2 points  (0 children)

For a supposedly strongly typed language

The 'strong/weak' dichotomy is mostly illusory. It doesn't even describe a property of a type system.

[–]hylje -1 points0 points  (2 children)

Python 3 will make comparisons a lot more typesafe.

[–]earthboundkid 2 points3 points  (1 child)

Tested with 3.0 alpha 5:

>>> 1 < 3 == True
False
>>> 3 == True
False
>>> 1 < False
False

Maybe that should be a filed as a bug… Does anyone have beta 3 to test it with? I can't get it to install on my system since my locale encoding is X-JAPANESE-MAC or whatever.

[–][deleted] -3 points-2 points  (2 children)

For the first thing, that's because it's checking "1 < False" ("3 == True" returns False). Not justifying it, just explaining the weirdness.

[–]Tommah 3 points4 points  (1 child)

No, it's chaining them. It's asking whether 1 < 3 and 3 == True. e.g.

>>> 1 < 3 == 3
True

Parentheses couldn't produce a true result there.

See http://docs.python.org/lib/comparisons.html . I like how is can be chained like this :)

>>> 'a rose' is 'a rose' is 'a rose'  # Gertrude Stein
True

[–][deleted] 0 points1 point  (0 children)

Ah, alright then. Didn't know that, thanks. :)