Operator Precedence/Boolean Logic
Antoon Pardon
antoon.pardon at rece.vub.ac.be
Thu Jun 23 07:15:32 EDT 2016
Op 23-06-16 om 12:59 schreef Marko Rauhamaa:
> Antoon Pardon <antoon.pardon at rece.vub.ac.be>:
>
>> Op 23-06-16 om 11:53 schreef Marko Rauhamaa:
>> Maybe something like this:
>>
>> def empty(sq):
>> try:
>> iter(sq).next()
>> except StopIteration:
>> return False
>> except:
>> raise TypeError
>> else:
>> return True
> That may or may not be as effective as a boolean check. The point is,
> Python has already declared that __bool__ is the canonical emptiness
> checker. You could even say that it's the principal purpose of the
> __bool__ method.
I think it is wrong to say __bool__ is the canonical emptiness checker.
It can be used for anything where you somehow think it is reasonable
to make a distinction between truthy and falsy. Even when talking
about emptyness doesn't make sense.
The function above at least raises an exception in a lot of cases
where the class provides booly behaviour yet emptyness wouldn't make
sense.
Would it be worth while? That you have to decide for yourself.
--
Antoon.
More information about the Python-list
mailing list