Question about __getattribute__/__getattr__

Joao Prado Maia jpm at papercut.org
Mon Sep 30 11:30:55 EDT 2002


Hi,

I'm trying to do the following:

class Real_Class:
    def real_method(self, name):
        return name

class Cache:
    r = None

    def __init__(self):
        self.r = Real_Class()

    def __getattr__(self, attr):
        cached_value = self.get_cache(attr)
        if cached_value == None:
            # need to eventually save the returned value in the cache
            return getattr(self.r, attr)
        else:
            return cached_value

    def get_cache(self, attr):
        return None

if __name__ == '__main__':
    t = Cache()
    print t.real_method('spam')


Now, the code works correctly - it calls the appropriate method name with 
the argument 'name'. However, I would like to cache the return value of 
the method call for a specific match of method name / method arguments. 
That is, I would cache:

t.real_method('spam')

And:

t.real_method('eggs')

in two different cache files.

It all comes down to the question: how do I know the full list of 
arguments passed to a function/method ? This way I'll be able to md5() the 
full string and have a different cache file for each method call.

Any ideas ?

Cheers,
Joao





More information about the Python-list mailing list