4 ms·
I think that's inherited from C which itself inherits it from various CPU architectures (although I guess nowadays many CPU architectures are tailored for machi
by simias 13y ago
I think that's inherited from C which itself inherits it from various CPU architectures (although I guess nowadays many CPU architectures are tailored for machine code generated by C compilers...).
I agree that for higher level languages capable of offering a true "nil" (and not a macro for 0 like NULL in C) it makes sense to make all other values "truthy".
I'm not a python coder but it surprises me it doesn't force comparisons explicitly like Java. After all "explicit is better than implicit".
That being said, what's the point of having nil and false? Seems a bit redundant to me.
- leoc 13y ago> That being said, what's the point of having nil and false? Seems a bit redundant to me. D: Here's a good reason to begin with: https://en.wikipedia.org/wiki/Open_world_assumption https://en.wikipedia.org/wiki/Open_world_assumption (In case wiki doeesn't make it clear enough, the CWA is squeaking mad in any situation where you cannot, in fact, determine the truth value of every proposition. Just because SQL does it doesn't make it rational.)
- ufo 13y agoC treats 0 as falsy so I would say that its more likely that Pythons behaviour is based on it - the Ruby and Clojure way has more to do with Scheme > That being said, what's the point of having nil and false? Seems a bit redundant to me. In some languages, like Common Lisp, nil and false are the same thing. However, it can be useful to separate the "this is a false value" from "this value does not exist", specially in a dynamically typed language. For example, in Lua you get a "nil" if you access a table field that does not exist and you delete a table field if you store null to it. Before they added a separate boolean type, it was really annoying to work with boolean arrays because storing nil in the array would make it full of holes and would confuse most library functions.
- sireat 13y agoI do not remember C(at least C89) having false at all, so I do not see that Python would base its behaviour on C.
- ufo 13y agoBoth C and Python treat the number 0 as falsy if you use it in an if statement. On the other hand, Ruby and Clojure treat 0 as truthy if you pass it in an if statement if(0){ //Ruby and Clojure go here }else{ //C and Python go here }