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
  What is all the info in the info window in Idle? Pedroski55 3 647 Jul-08-2023, 11:26 AM
Last Post: DeaD_EyE
  Subclass initialized property used in parent class method. Is it bad coding practice? saavedra29 5 1,679 Feb-07-2022, 07:29 PM
Last Post: saavedra29
  ABC Module and @property decorator, Pythonic Way? muzikman 21 5,491 Aug-18-2021, 06:08 PM
Last Post: muzikman
  @property vs __set__ / __get__ and __setattr__ / __getattr__ okhajut 1 3,252 Jun-15-2021, 03:48 PM
Last Post: snippsat
  Can property getters and setters have additional arguments? pjfarley3 2 2,989 Oct-30-2020, 12:17 AM
Last Post: pjfarley3
  Property price calculation oli_action 4 3,091 Jul-15-2020, 04:27 PM
Last Post: sridhar
  Use of @property decorator ruy 16 6,372 Jun-09-2020, 05:29 PM
Last Post: buran
  Problem adding keys/values to dictionary where keynames = "property" and "value" jasonashaw 1 2,014 Dec-17-2019, 08:00 PM
Last Post: jasonashaw
  strange class property KaliLinux 2 2,310 Nov-25-2019, 04:32 PM
Last Post: KaliLinux
  print all method and property of list object engmoh 4 2,798 Oct-26-2019, 05:33 PM
Last Post: engmoh

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020