Hacker News new | ask | show | jobs
by gtaylor 4704 days ago
Ditto for Python (int 0 at least).
3 comments

Yeah, but "0" is True. Why would you store a name as an int?
In python 0 is falsy. "0" is not.
Weird, because makes a distinction between int and string.
Python makes a distinction between types, but it has a concept of falsyness (as do many languages, though it is often more restricted e.g. in Ruby only false and nil are falsy IIRC).

Falsyness is used in boolean-ish contexts (if, while and explicit boolean conversion), by default all of None (null), False, 0, 0.0, "" (the empty string, unicode or binary) and empty collections ([], (), {}, set()) are falsy and although UDTs are "truthy" it can be overridden.

This has nothing to do with making the distinction between ints and strings. You could actually implement the same thing in e.g. Haskell (by creating a "Booleanish" typeclass and implementing it on all the types you care for).

edit: please note that — in Python — 0 (the integer) is falsy but "0" (the string containing a single character 0x30) is truthy. I expect gtaylor talks about bugs in handling of IDs or sequence numbers and the like, not usernames. I know I've hit them when not being very attentive.

GForth uses 0 for 'false' but -1 for 'true'... that confuses me a lot.
I expect that it's because in twos complement representation -1 is represented as all bits set.
I suppose it's because then it can just compare the first bit.
BASIC does that, too.