Python: Simplify code in a couple of places. Copy Andrew's changes from g53b5166.