Python Forum
How do I get the info for a cached property?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I get the info for a cached property?
#1
Say I have the following class with a property that has an expensive operation:

from functools import lru_cache class Example: @property @lru_cache(maxsize=None) def a_getter(self): print('Doing an expensive operation!') expensive_result = 5 return expensive_result test = Example() for i in range(0, 1000): a_variable = test.a_getter print(test.a_getter.cache_info())
As evident, this does not run with 3.6.1, with the traceback:

Error:
Traceback (most recent call last): File "python", line 16, in <module> AttributeError: 'int' object has no attribute 'cache_info'
However, if I remove the property decorator, then the code works (I just have to switch to a function call for the getter).

Is there a way for me to get this cache info with the property decorator as well?
Reply
#2
Maybe not the best, but
from functools import lru_cache class Example: @property def a_getter(self): return self._a_getter() @lru_cache(maxsize=None) def _a_getter(self): print('Doing an expensive operation!') expensive_result = 5 return expensive_result test = Example() for i in range(0, 1000): a_variable = test.a_getter print(test._a_getter.cache_info())
would be interesting to see other suggestions
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Use
print(Example.a_getter.fget.cache_info())
Reply
#4
Thanks guys! Both of your solutions worked, but @Gribouillis's solution fits my needs better. Big Grin

(Also unrelated question, but does this site have a method of marking threads solved or anything, by any chance?)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [openpyxl] Set the locking property of all cells BelleroDev 3 881 Nov-07-2025, 11:59 AM
Last Post: Pedroski55
  Invalid Date Format fo Cached Files jland47 1 1,589 May-22-2024, 07:04 PM
Last Post: deanhystad
  What is all the info in the info window in Idle? Pedroski55 3 2,171 Jul-08-2023, 11:26 AM
Last Post: DeaD_EyE
  ABC Module and @property decorator, Pythonic Way? muzikman 21 11,172 Aug-18-2021, 06:08 PM
Last Post: muzikman
  @property vs __set__ / __get__ and __setattr__ / __getattr__ okhajut 1 6,785 Jun-15-2021, 03:48 PM
Last Post: snippsat
  Can property getters and setters have additional arguments? pjfarley3 2 5,090 Oct-30-2020, 12:17 AM
Last Post: pjfarley3
  Property price calculation oli_action 4 5,383 Jul-15-2020, 04:27 PM
Last Post: sridhar
  Use of @property decorator ruy 16 13,253 Jun-09-2020, 05:29 PM
Last Post: buran
  Problem adding keys/values to dictionary where keynames = "property" and "value" jasonashaw 1 3,089 Dec-17-2019, 08:00 PM
Last Post: jasonashaw
  strange class property KaliLinux 2 3,577 Nov-25-2019, 04:32 PM
Last Post: KaliLinux

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.