Pythonic emptiness checking

MRAB google at mrabarnett.plus.com
Wed Apr 29 21:21:22 EDT 2009


Carl Banks wrote:
> On Apr 29, 6:46 am, Bruno Desthuilliers <bruno.
> 42.desthuilli... at websiteburo.invalid> wrote:
>> 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.
> 
> I think a better answer to this question is: "The Zen of Python is not
> called the Cold Hard Rules of Python"; in this case the language goes
> against this particluar Zen as it does in many other places.
> 
"Although practicality beats purity". It's all about balance.



More information about the Python-list mailing list