LWN.net Logo

Why is it a mess?

Why is it a mess?

Posted Dec 19, 2011 13:18 UTC (Mon) by ekj (guest, #1524)
In reply to: Why is it a mess? by HelloWorld
Parent article: Cracks in the Foundation (PHP Advent)

If the language is object-oriented and allows objects to override operators, it makes sense to also let them answer the question of being true or false.

This is essentially what happens in python: An empty string is considered false while all other strings are considered true. That's just sugar offcourse, you could do if mystring.length() > 0 instead of if mystring and get the same result, but it's a useful shortcut.

Considering integers different from zero to be true, and zero to be false is also very common although the same tradeoff applies if myint could always be rewritten to if myint <> 0 which does have the advantage of being more explicit.

automagick type-conversion like PHP have is nuts though, they consider "0" to be false because the integer 0 is false. (but "0.0" is not false, despite the fact that 0.0 *is* false.


(Log in to post comments)

Why is it a mess?

Posted Dec 25, 2011 14:09 UTC (Sun) by juliank (subscriber, #45896) [Link]

> myint <> 0

Use myint != 0. That other form is deprecated in Python 2, and removed in Python 3.

Copyright © 2013, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds