Skip to content

caching of methods updating instance variables #431

@smidm

Description

@smidm

I haven't found an elegant way to cache methods updating instance variables:

class Foo(object):

    def __init__(self, args):
        self.data = None

    def compute(self, ...):
        ...  # long computation
        self.data = result

One can create a cached private method that is returning computed data, but in this point the caching mechanism is already complicating the code:

class Foo(object):

    def __init__(self, args):
        self.data = None
        self.__compute__ = mem.cache(self.__compute__)        

    def compute(self, ...):
        ...
        self.data = self.__compute__(...)
        
    def __compute__(self, ...):
        ...  # long computation
        return result  

Proposal: a possible solution could be a specification of altered instance variables in the decorator:

self.compute = mem.cache(self.compute, [self.data, ...]) 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions