Metaclasses vs. standard Python reflection?

Pedro Rodriguez pedro_rodriguez at club-internet.fr
Tue May 6 09:37:54 EDT 2003


On Tue, 06 May 2003 09:51:52 +0200, Stephan Diehl wrote:

> Dave Benjamin wrote:
> 
>> In article <sSIta.82275$K35.2431544 at news2.tin.it>, Alex Martelli wrote:
>>> Not really, because THE "key concept" in AOP is that you do this
>>> *transversally* with respect to inheritance hierarchies -- modifying
>>> existing classes (without changing their source code), rather than
>>> creating new classes by inheritance, and ACROSS a swathe of classes
>>> not necessarily related by inheritance.
>> 
>> Is this the sort of thing you can do with metaclasses? If so, how? I
>> feel pretty comfortable with what I know of AOP, but I still feel very
>> confused about metaclasses and if/how they propose a solution to this
>> problem.
> 
> Please have a look at:
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/198078
> 
> I don't know AOP, so it's probably not an AOP example, but it shows, how
> you can change behaviour with a metaclass and not touching the original
> class definition.
> 

I disagree with you when you say that you don't touch the actual code of
the class, since you have to introduce the 'metaclass' inside the class
definition (or at last at module level of the class definition). 

The problem with metaclasses, AFAIK, is that they are intrusive. Can you
apply your 'metaclass logger' to the classes of the standard library
without modification ? I don't think so. Still this is not a problem, as
long as you develop a framework where you're in control of all objects
you need to 'observe'.

Another limitation is that this only apply to classes, where the ability
of intercepting calls could be also be usefull with bare function, or at
instance level (i.e. for some instances of a class).

Pedro




More information about the Python-list mailing list