Implicit conversion to boolean in if and while statements

Rick Johnson rantingrickjohnson at gmail.com
Fri Feb 8 01:22:01 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.
> 
> 
> 
>     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