Implicit conversion to boolean in if and while statements

alex23 wuwei23 at gmail.com
Tue Jul 17 21:35:09 EDT 2012


On Jul 17, 6:23 pm, Andrew Berg <bahamutzero8... at gmail.com> wrote:
> On 7/17/2012 2:08 AM, Steven D'Aprano wrote:
> > The default behaviour is that every object is something, hence true-like,
> > unless explicitly coded to be treated as false-like. Since both loggers
> > and functions are objects, they are true-like unless the default is
> > overridden.
>
> I am aware of the default behavior, but the reason for it still eludes me.

Because it makes it simple to distinguish between having an object and
not having one without having to explicitly test for it each time.

    db = connect("my:db") # or None if the connection failed
    if db:
        <do something>

I find that usage to be incredibly intuitive.



More information about the Python-list mailing list