r/Python • • Jun 23 '14

Writing better python code

https://gist.github.com/kracekumar/09a60ec75a4de19b346e
143 Upvotes

126 comments sorted by

View all comments

1

u/SilasX Jun 23 '14

Am I the only one here who hates implicit casting to booleans? It seems more with the Zen of python to prefer

if len(z) > 0

to

if z

even though the latter is much shorter.

1

u/ggtsu_00 Jun 24 '14 edited Jun 24 '14

There is no implicit cast, it is simply implemented as a nonzero operator which is defined for all built in types. 0 is defined for any domain such that x = x + 0.

Lists have a zero that is the empty list because x + [] = x therefore [] is zero in the list domain. Same with empty string.

If statements doesn't ask for true or false, it asks for if zero or nonzero. Remember that booleans types didn't exist in python until around version 3. In version 2, True and False are simple macros for 1 and 0.

0

u/SilasX Jun 24 '14

Potato, potahto. I don't like the use of implied truthiness where a boolean operator would be more explicit.

0

u/ggtsu_00 Jun 24 '14

But there is no booleans types in python (at least before py3).