Pythonic emptiness checking

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Wed Apr 29 15:46:33 CEST 2009

Filip Gruszczyński a écrit :
> One of the Python Zen rules is Explicit is better implicit. And yet
> it's ok to do:
> if x:
>    do_sth
> when x is string or list. Since it's very comfy, I've got nothing
> against though. I am just curious, why is it so?

Because it is explicit (or at least considered as such) that in Python, 
an empty list or string (as well as empty tuples, dicts and sets and 
numeric zeros, False and None) have a false value in a boolean context.

> And one more thing: is it ok to do
> if x:
> instead of
> if x is not None:

Depends on the context. You of course understand that the two 
expressions are not equivalent, don't you ?-)

More information about the Python-list mailing list