6 ms·
It's a neat approach, but I can anyone give explicit reasons as to why using a function instance variable is "bad"?
by artlogic 17y ago
It's a neat approach, but I can anyone give explicit reasons as to why using a function instance variable is "bad"?
- arebop 17y agoGlobal data is bad; only the memoization code needs to access the cache; why expose the cache more broadly than that? More explicitly, one problem with overexposure is namespace collisions.
- artlogic 17y agoGlobal data can be bad, but a function instance variable isn't global - it's simply public. Using closures to simulate private variables has always seemed like overkill to me, but I tend to prefer Python's approach to private variables. Also, being able to manipulate the cache from outside the function could be helpful in certain cases where a full fledged cache object would be overkill. I do agree that namespace collision is certainly an issue, but it's traditionally resolved by mild name mangling (again, see Python). In my opinion, the advantages of a less clever (read easily understood) public implementation outweigh the disadvantages.