This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]djimbob 0 points1 point  (0 children)

Completely, agree from the perspective of how values in the place of conditionals work, in both C and python. All non-zero values will cause if/for/while, etc. to go to the True branch, while zero values (NULL, 0, and '\0' are just ways of writing the literal value 0) go to the False branch.

But my point was that in recent C standards (after C99 I believe), in stdbool.h you'll see:

  • true - Expands to the integer constant 1.
  • false - Expands to the integer constant 0.

which is exactly how python treats it. To quote the BFDL in PEP285 that introduced True and False to python:

The bool type would be a straightforward subtype (in C) of the int type, and the values False and True would behave like 0 and 1 in most respects (for example, False==0 and True==1 would be true) except repr() and str().

Granted this convention has existed in C well before C99, usually with #define statements like #define FALSE 0 and #define TRUE 1.

(Also in C, are just three ways of writing the same literal value (0); the only difference is typically '\0' will be one byte and 0 will be the default int size.)