Question about __getattribute__/__getattr__
Joao Prado Maia
jpm at papercut.org
Tue Oct 1 09:23:22 EDT 2002
On Tue, 1 Oct 2002, holger krekel wrote:
> For caching a function's results why do you need __getattr__?
>
Because with __getattr__ it will work transparently, even if I add new
methods to the cached class, or change the name of an existing one.
Instead of having to create one method for each method in the cached
class, I just use __getattr__ to receive all calls and apropriately cache
the results.
> It seems you really need another caching policy than the prescribed
> memoize pattern. For example, you may implement a dictish object that evicts
> old cached values by means of counting accesses, oldest access times
> or whatever. I still see no use for expecting __getattr_ to know about
> the arguments. Not?
>
A dictionary approach is not good enough for my needs, as it will
eventually hold too much data in memory. As I explained before, saving the
method return values in md5()'ed named files is the best solution I can
think of.
But going back to the subject, I need the arguments on __getattr__ so I
can create unique filenames for the method call signatures. That is,
calling a method like this:
t.real_method('spam')
would create a filename as:
filename = md5.md5().digest('%%' % ('real_method', 'spam'))
While the following method call:
t.real_method('eggs')
would create a filename as:
filename = md5.md5().digest('%%' % ('real_method', 'eggs'))
This way you can see that both method calls would be cached in different
files. To see if the method call was already cached, I just need to see if
the file exists.
I hope this explained a little better the rationale behind the question.
:)
Cheers,
Joao
More information about the Python-list
mailing list