python: closure scoping and dealing with text