[Tutor] Intercepting methods calls

Alan Gauld alan.gauld at btinternet.com
Wed Jun 4 21:23:45 CEST 2008

"Marilyn Davis" <marilyn at deliberate.com> wrote

> > Great question, Alan.  But, whatever the answer, I'd like to see 
> > the
> > variations listed, if you have time.
> Well, let's start the ball rolling then.
> __getattribute__
> __getattr__
> Metaclasses
> decorators
> properties
> __init__
> The basic working is that you've got a function, and that get's 
> wrapped.

One more:

You can write an explicit dispatch method (often called send) with a
dictionary lookup of the methods. This is usually done as a class 
This is the classic approach in languages like C++ without dynamic
introspection. Its also somewhat similar to Objective Cs approach.
But it could be done in Python too.

The send method simply delegates the call to the approproate real
method via a lookup table. Thus allows some of the fancier Lisp or 
style tricks like multiple dispatch, multi-methods, multi-named 
changing calling sequence in inheritance etc etc.

But it requires much more work in the class definition of course, so
should be used sparingly!!!   I've mainly used it in dynamic database
creation (mapping new table or column names to generic getter/setter
methods) and in AI applications using C++.

Alan G 

More information about the Tutor mailing list