[Python-ideas] Please reconsider the Boolean evaluation of midnight

Ryan Hiebert ryan at ryanhiebert.com
Wed Mar 5 16:55:13 CET 2014


On Wed, Mar 5, 2014 at 9:42 AM, Paul Moore <p.f.moore at gmail.com> wrote:

> On 5 March 2014 15:08, Yann Kaiser <kaiser.yann at gmail.com> wrote:
> >  Let me show you an actual zero:
> >
> >     if event.num_attendants:
> >         prepare_cake()
> >     else:
> >         cancel_event()
> >
> > When no one is coming to your party, is is clearly a different
> > condition than if any number of people are coming to your event.  When
> > you read "if num_attendants", you can clearly tell this is going to do
> > something depending on if people are coming or not.
>
> I'm sorry, are you really trying to say that the above code is better than
>
>     if event.num_attendants != 0:
>         prepare_cake()
>     else:
>         cancel_event()
>
> ?
>
> (Personally, I'd actually prefer something like "if
> event.num_attendants > 0" or switch the order of clauses and test for
> being equal to 0, but that's a minor issue. The major point is I'd
> prefer any of these to your version).


I suspect you'd like the whole idea of empty collections and zeroes
evaluating false to go away, to force this kind of style (one right way to
do it and all). Some languages do that. Python isn't one of them.

I can see both sides, but I like how Python can use empty lists and zeros
as false values, and I often (but not always) write code that takes
advantage of it.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140305/3f02e5cf/attachment.html>


More information about the Python-ideas mailing list