Metaclasses vs. standard Python reflection?

Stephan Diehl stephan.diehl at gmx.net
Tue May 6 12:05:09 EDT 2003


Pedro Rodriguez wrote:

> On Tue, 06 May 2003 09:51:52 +0200, Stephan Diehl wrote:
>
 
[...]

>> 
>> 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).

O.K., my wording might not have been as excact as it should have been. The
point (as far as I can see it) that there is only one entry point (the
'__metaclass__' classattribute) that changes some behaviour of the class.
The class itself will work (at least in that example), without the
metaclass.

> 
> 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.

Isn't this the whole point of metaclasses? To be intrusive?
The nice thing about meta classes (my very private opinion :-) is that the
actual (intrusive) code can be located at a very precise point: the
metaclass definition, and is not spread out all over the actual class
definition.
 
> 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'.

Yes.

> 
> 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).

You are probably right. Please keep in mind that my example was just that:
an example.


Thanks a lot for your feedback

Stephan

> 
> Pedro





More information about the Python-list mailing list