Reputation: 466
In this documentation about lru_cache
I can see that there's the possibility to call a function on a decorator with the dot notation, for instance I can call:
lru_cache_decorated_func.cache_info()
What I'm trying to achieve is make my own decorator with my custom function to call that works and is called like cache_info()
.
So how I can add such a function to a decorator?
Upvotes: 0
Views: 416
Reputation: 42492
A decorator is nothing more (or less) than a callable returning a callable[0] aka
@foo
def bar():
...
is exactly the same as:
def bar():
...
bar = foo(bar)
There are various options to "smartify" decorators, what lru_cache does is pretty simple:
wrapper
functionwrapper
functionimport functools
def foo(fn):
@functools.wraps(fn)
def wrapper(*args, **kwargs):
return fn(*args, **kwargs)
wrapper.thing = 'yay'
return wrapper
@foo
def bar(): ...
print(bar.thing)
will print yay
.
[0] or even a not-callable, as is the case for @property
or @cached_property
.
Upvotes: 2