Implicit conversion to boolean in if and while statements

Rick Johnson rantingrickjohnson at gmail.com
Fri Feb 8 01:29:37 EST 2013


On Tuesday, July 17, 2012 8:35:09 PM UTC-5, alex23 wrote:
> 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.

That's a strange thing to say when you go on to provide an example that tests the validity of the object "each and every time":

>
>     db = connect("my:db") # or None if the connection failed
>     if db:
>         <do something>
> I find that usage to be incredibly intuitive.

And i find it to be incredibly asinine.

Consider this:

    if connect("my:db") as db:
        <do something>

No need to make a call and then test for the validity of the call when you can do both simultaneously AND intuitively.

*school-bell-rings*



More information about the Python-list mailing list