I find it very handy to check if an object is “empty” with the following construct:
l=[]
if l:
do_stuff()
For a standard python list, the if will be executed only if the list is not empty.
My question is, how can I implement the same idea for my own objects?
Define a method
__bool__(Python 3.x) or__nonzero__(2.x). Or define both for portability, with one returning the result of the other.