Why bool( object )?

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Wed Apr 29 20:29:33 EDT 2009


On Wed, 29 Apr 2009 17:35:28 +0200, Marco Mariani wrote:

> Bruno Desthuilliers wrote:
> 
>> Lawrence D'Oliveiro a écrit :
> 
>>>> What is the rationale for considering all instances true of a user-
>>>> defined type?
>>>
>>> It's a stupid idea,
>> 
>> Nope, it's a very sensible default (given you can redefine the
>> 'nothingness' value of your types instances), specially when the
>> language doesn't have a proper boolean type (which was the case for
>> Python until 2.2 or 2.3, can't remember exactly).
> 
> Man, you've given a serious answer to a sarcastic reply to an OP who has
> been -- for years -- second in trolliness only to Xah Lee.
> 
> Either that, or I have to replace my humor detector.

Aaron (castironpi) certainly used to be a major troll, but he got better. 
I know, I know, sometimes it's hard to tell if he's trolling or just 
engaging in flights of fancy, but I think it's mostly the later.

Lawrence's reply wasn't sarcastic -- he really does think that having 
every object be meaningful in a boolean context is stupid. 

I think he's not just wrong, but badly wrong. To me, "if bool(x):" is 
barely better than "if bool(x) == True:", which in turn is not much 
better than "if (bool(x) == True) == True:".

The reason why Lawrence's insistence is so badly wrong becomes more 
apparent if you look at what you can do with boolean contexts other than 
simple `if` tests. Compare:


for x in a or b or c:
    do_something_with(x)


versus:


if len(a) > 0:
    temp = a
elif len(b) > 0:
    temp = b
elif len(c) > 0:
    temp = c
for x in temp:
    do_something_with(x)




-- 
Steven



More information about the Python-list mailing list