Expression can be simplified on list
Rustom Mody
rustompmody at gmail.com
Thu Sep 29 02:47:01 EDT 2016
On Thursday, September 15, 2016 at 1:43:05 AM UTC+5:30, Terry Reedy wrote:
> 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.
>
Not sure what you are trying to say Terry...
Your English suggests you disagree with me
Your example is exactly what I am saying; if a type has a behavior in which
all values are always True (true-ish) its a rather strange kind of bool-nature.
Shall we say it has Buddha-nature? ;-)
> 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.
Sure one can always (ok usually) avoid a bug in a system by not using the
feature that calls up the bug. Are you suggesting that that makes the bug non-exist?
In more detail:
- If user/programmer defines a new type
- Which has no dunder bool
- Which has no dunder len
- Which has no ... (all the other things like len that can make for a
non-trivial bool behavior)
- And then uses a value of that type in a non-trivial bool-consuming position
such as the condition of an if/while etc
There's a very good chance that bool-usage is buggy
In more mundane terms, dunder bool defaulting to true is about as useful
as if it defaulted to
2*random.random()
Why not default it in the way that AttributeError/NameError/TypeError etc
are raised?
More information about the Python-list
mailing list