Patch __call__ of a function
问题 I need to patch current datetime in tests. I am using this solution: def _utcnow(): return datetime.datetime.utcnow() def utcnow(): """A proxy which can be patched in tests. """ # another level of indirection, because some modules import utcnow return _utcnow() Then in my tests I do something like: with mock.patch('***.utils._utcnow', return_value=***): ... But today an idea came to me, that I could make the implementation simpler by patching __call__ of function utcnow instead of having an