Implicit conversion to boolean in if and while statements

Terry Reedy tjreedy at udel.edu
Tue Jul 17 13:57:19 EDT 2012


On 7/17/2012 4:23 AM, Andrew Berg 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.

Ultimately, because Guido's intuition said that the current behavior is 
the best. I happen to agree on this one.

Quoting from elsewhere:
 > If it truly is about something vs. nothing, why is a NameError (or
 > AttributeError) raised when testing with an undefined variable?


Python does not have 'undefined variable' objects in the way that some 
other languages do. There is nothing to test 'with'. 'if 
undefined_name:' raises NameError because all uses of undefined_names 
other that assignments do. The actually execution order is expression 
first, then if. You can explicitly test "'some_name' in namespace" or 
"hasattr(obj, 'somename') by turning the possibly undefined name into a 
string object.

-- 
Terry Jan Reedy






More information about the Python-list mailing list