It is not cached as you assume. The statement you show that uses f() is a top-level statement and therefore it only runs once when the module is first imported. It doesn't matter how many times you expect a to equal 1, you're not re-executing any code whatsoever. The only time that the code in the ./asd module ran was when the module was first imported, when zero test cases had been executed.