Why bool( object )?

Aaron Brady castironpi at gmail.com
Tue Apr 28 14:59:18 EDT 2009


On Apr 28, 2:39 am, Steven D'Aprano
<ste... at REMOVE.THIS.cybersource.com.au> wrote:
> On Mon, 27 Apr 2009 23:11:11 -0700, Aaron Brady wrote:
> > What is the rationale for considering all instances true of a user-
> > defined type?  Is it strictly a practical stipulation, or is there
> > something conceptually true about objects?
>
> Seven years ago, in an attempt to convince Guido *not* to include
> booleans in Python, Laura Creighton wrote a long, detailed post
> explaining her opposition.
>
> At the heart of her argument is the observation that Python didn't need
> booleans, at least not the ints-in-fancy-hats booleans that we've ended
> up with, because Python already made a far more useful and fundamental
> distinction: between Something and Nothing.
>
> http://groups.google.com/group/comp.lang.python/msg/2de5e1c8384c0360?...
>
> All objects are either Something or Nothing. The instances of some
> classes are always Something, just as the instances of some classes are
> always Nothing. By default, instances are Something, unless __nonzero__
> returns False, or __len__ returns 0, then they are Nothing.
>
> In a boolean (or truth) context, Something and Nothing behave like True
> and False in languages with real booleans:
>
> if obj:
>     print "I am Something"
> else:
>     print "I am Nothing"
>
> To steal an idiom from Laura: Python has a float-shaped Nothing 0.0, a
> list-shaped Nothing [], a dict-shaped Nothing {}, an int-shaped Nothing
> 0, a singleton Nothing None, and so forth.

The sound of that metaphor is rather pleasing ('sweet nothings'), but
I'm not so sure that metaphors belong in computer science and
programming.  Nothing can't have many shapes.  Having no onions is the
same as having no carrots.  If the different shapes of nothing don't
compare equal to each other, which they don't, then they aren't all
the same thing in different shapes.

Furthermore, it is awfully presumptuous to give objects a default
'nothing-ness' of 'something'.  If anything, they should be nothing by
default.  Conversion to other primitives is very picky; why should
Boolean be so tolerant?



More information about the Python-list mailing list