Now that I think about it, this line should actually just be

    if entity:

Yours works, but this is shorter and also a little faster.