Operator Precedence/Boolean Logic

Marko Rauhamaa marko at pacujo.net
Thu Jun 23 06:59:54 EDT 2016


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.


Marko



More information about the Python-list mailing list