Expression can be simplified on list

Terry Reedy tjreedy at udel.edu
Wed Sep 14 16:12:14 EDT 2016


On 9/14/2016 3:16 AM, Rustom Mody wrote:

> In THOSE TYPES that element can justifiably serve as a falsey (empty) type
>
> However to extrapolate from here and believe that ALL TYPES can have a falsey
> value meaningfully, especially in some obvious fashion, is mathematically nonsense.

Python make no such nonsense claim.  By default, Python objects are truthy.

 >>> bool(object())
True

Because True is the default, object need not and at least in CPython 
does not have a __bool__ (or __len__) method.  Classes with no falsey 
objects, such as functions, generators, and codes, need not do anything 
either.  In the absence of an override function, the internal bool code 
returns True.

It is up to particular classes to override that default and say that it 
has one or more Falsey objects.  They do so by defining a __bool__ 
method that returns False for the falsey objects (and True otherwise) or 
by defining a __len__ method that returns int 0 for falsey objects (and 
non-0 ints otherwise).  If a class defines both, __bool__ wins.

-- 
Terry Jan Reedy




More information about the Python-list mailing list