Re:Anti-Patterns in Python Programming
by ollysb on Thursday July 10, 2014 @10:37AM
(#8015093)
Why not just memoize the result of the slow function?
[ Parent
| on HN ]
I think by your use of "just memoize" that I didn't explain myself well enough. I don't mean to focus on the slow aspect, nor even the function aspect. It could be
def foo(default_arg = [0]*(256*256)):
...
and still get the same namespace issues.Memoization is not always going to be an available solution. For example, it may be that slow_f() returns a stateless object, so can be reused, while slow_f(x) returns something stateful. You can think of my examples as either using default arguments as a single element memo, or using a module variable for the same. Both premised on the idea that the developer knows enough to make the right decision.