boolean from a function

Grant Edwards invalid at invalid.invalid
Tue Dec 13 12:20:35 EST 2011


On 2011-12-13, Andrea Crotti <andrea.crotti.0 at gmail.com> wrote:

> Now somewhere else I had
>
> if func_bool:
>      # do something
>
> I could not quite understand why it was always true, until I finally
> noticed that the () were missing. Is there some tool to avoid these
> stupid mistakes? (pylint doesn't warn me on that) I don't think I
> will ever (or almost) have to use a function as a boolean, instead of
> its return value...

FWIW, I have do use the truth value of a function (rather than it's
return value) when writing code that uses callbacks:

def foo(callback=None):
    # do some stuff
    if callback:
        callback()
    # do some more stuff        

It probably would be better to use "if callback is not None:", but I
find I usually skip "is not None".
    
-- 
Grant Edwards               grant.b.edwards        Yow! Will it improve my
                                  at               CASH FLOW?
                              gmail.com            



More information about the Python-list mailing list