Implicit conversion to boolean in if and while statements

alex23 wuwei23 at gmail.com
Wed Jul 18 03:35:09 CEST 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