Overriding all methods in a class

laotseu bdesth at nospam.free.fr
Mon Sep 23 00:38:16 EDT 2002


Joao Prado Maia wrote:
> Hi,
> 
> I have been looking for an answer for this question for a while, but 
> couldn't find anything that would work for what I want to do.
> 
> I know that you can use __getattr__ to overload methods that your class 
> don't have, but what about overloading all methods, so I could in a way 
> 'intercept' calls to my methods and do something different if I wanted, is 
> that possible ?
> 
> What I want to do basically is create a caching system for a specific 
> class, and have __getattr__ (or any other magic method name) handle the 
> decision to sue the cached return value or to actually call the method.
> 
> Maybe it would be just easier to rename my current class to _Classname and 
> create a new Classname with just __getattr__.
> 
> Any suggestions ?
> 
> Cheers,
> Joao
> 
> 

from stupidQ import whyBlackMagic

Well, if it's only about caching, why do you need to go into 'black 
magic' to implement it ? Couldn't you just handle the cache etc in the 
method itself ?


laotseu




More information about the Python-list mailing list