Operator Precedence/Boolean Logic
Jussi Piitulainen
jussi.piitulainen at helsinki.fi
Thu Jun 23 08:05:50 EDT 2016
Antoon Pardon writes:
> 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.
It also *changes* many things where emptiness *would* make sense. In
particular, it can first *make* a thing empty and then happily declare
it not empty. Not good.
Is "sq" mnemonic for something?
More information about the Python-list
mailing list