Why bool( object )?

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Apr 28 12:12:43 EDT 2009


En Tue, 28 Apr 2009 09:22:01 -0300, Colin J. Williams <cjw at ncf.ca>  
escribió:
> Lie Ryan wrote:
>> Aaron Brady wrote:

>>> This makes it so all objects except False, None, 0, and empty
>>> containers are true by default.  I am not convinced that 'if <a
>>> generic object>' should have any meaning; it should probably throw an
>>> exception.  Is it part of Python's look and feel or its mentality?  Is
>>> it part of the Zen?  Certainly other ideal types can't be cast from
>>> generic objects, so why booleans?  Is it an ineffable component of the
>>> author's vision for the language?  I think that giving arbitrary
>>> syntactic constructs meaning is just space-filler.  It's worse than
>>> syntactic sugar, it's semantic sugar.  Why not assign meanings willy-
>>> nilly to other random juxtapositions of tokens?

>>  It's part of the design decision. In almost all cases (in any  
>> language), a so-called "Design Decision" is rather random and prone to  
>> subjective judgment, just as the decision to make bool(int) returns  
>> False only on 0, -1, or for all negative values; whether to make  
>> bool(100) and exception or True; or round() rounds down or up or  
>> even-odd; or the use of brackets vs. indentation; or whether to treat  
>> empty list as True or False.
>
> I'm puzzled by the last sentence:
>
>>>> bool(0)
> False
>>>> bool(-1)
> True
>>>> bool(-100)
> True

That's the "design decision". bool(-1) *might* have been False, and  
bool(-100) *might* raise an exception. They behave the way they do because  
of a conscious decision.

To Aaron: "Programming language design is not a rational science. Most  
reasoning about is is at best rationalization of gut feelings, and at  
worst plain wrong."
(GvR in python-ideas:  
http://groups.google.com/group/python-ideas/msg/ac61f03c32578bae )

-- 
Gabriel Genellina




More information about the Python-list mailing list