louisPy icon indicating copy to clipboard operation
louisPy copied to clipboard

Using the decorator with classes

Open reinoldus opened this issue 6 years ago • 2 comments

Hi,

if one wants to use the decorator with classes then one runs into the following issue:

The first arg of a function is "self", which will return the memory address if one calls "str()" on it.

This can be fixed adding the "str" magic methods to one's class:

def __str__(self):
    return "class_name"

A more general fix would be better in my opinion, basically, the "key" generation has to be altered:

Right now I have no better idea other than:

[str(i) for i in args if not "object at 0x" in str(args[0])]

Not sure if it would be a better approach to force the user to set the "str" by throwing an exception.

Let me know what you think.

reinoldus avatar Aug 23 '19 16:08 reinoldus

Thank you for pointing out this issue, it was the first thing I ran into when trying out this package. ;)

bdowling avatar Dec 02 '19 04:12 bdowling

The flask-cache module solves this by asking the user to define __repr__ of any returned objects or on the class of any methods which are cached

pbeart avatar Dec 19 '20 20:12 pbeart