Metaclasses vs. standard Python reflection?

Dave Benjamin ramen at lackingtalent.com
Tue May 6 03:11:32 EDT 2003


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.

For instance, all of the metaclass examples I've seen do something like this:

class Foo:
    __metaclass__ = Bar
    
    # etc.
    
But this seems to be doing something like subclassing. If the idea is to not
touch the original source and merely apply "aspects" to it, how would you do
this in Python? Could you do something like:

import Foo
import Bar
Foo.Foo.__metaclass__ = Bar.Bar

and get a new, improved Foo with Bar-like enhancements, or am I off track?

Thanks,
Dave




More information about the Python-list mailing list