Decorator with parameters
Chris Angelico
rosuav at gmail.com
Mon Apr 6 04:43:12 EDT 2020
On Mon, Apr 6, 2020 at 6:36 PM ast <ast at invalid> wrote:
>
> Hello
>
> I wrote a decorator to add a cache to functions.
> I realized that cache dictionnary could be defined
> as an object attribute or as a local variable in
> method __call__.
> Both seems to work properly.
> Can you see any differences between the two variants ?
>
There is a small difference, but it probably won't bother you. If you
instantiate your Memoize object once and then call it twice, one form
will share, the other form will have distinct caches.
memo = Memoize1(16) # or Memoize2(16)
@memo
def fib1(n): ...
@memo
def fib2(n): ...
The difference is WHEN the cache dictionary is created. That's all. :)
ChrisA
More information about the Python-list
mailing list