Hooks, aspect-oriented programming, and design by contract

Huaiyu Zhu huaiyu at gauss.almadan.ibm.com
Wed Jan 23 15:54:15 EST 2002


On Tue, 22 Jan 2002 16:59:12 +0100, Alex Martelli <aleax at aleax.it> wrote:
>"Pedro Rodriguez" <pedro_rodriguez at club-internet.fr> wrote in message
>news:pan.2002.01.22.16.08.43.505728.1637 at club-internet.fr...
>    ...
>> As Alex pointed, 'metaclasses' are maybe the next step. BTW, there are the
>> examples in Demo/metaclasses that may help, you'll even find an Eiffel
>> class implementation for pre/post conditions.
>
>I also point out that I don't know how to switch metaclasses dynamically,
>temporarily and "retroactively" (i.e. so that already-existing instances
>are also affected), as requested by the original poster.  I don't even
>know if such a metaclass-switch scheme is possible at all.

I'm trying to understand these discussions.  Let me see if I get this right.
Consider the following snippet

class A:
    def method1(self): pass
    def method2(self): pass
a = A()

The objected oriented techniques allow you to subclass A to add more
attributes, like

class B(A):
    def method3(self): pass
b = B()

But this will not affect class A and object a.  And if you want to change
all these methods in the same way, you have to modify them one by one.

In contrast, what aspect oriented techniques allow you to do is to crosscut
through different methods, so that you could, for example, let each of the
methods printout "calling <methodname>", in one stroke, without putting
print statements in each of them.  You could do this to class A or B or
selected subset of methods, and it will affect existing objects a and b.

If you let class A be derived from another class C, which itself is an
instance of a metaclass, then these aspects discussed above become the
properties of the metaclass, so you can achieve similar effect by using
metaclass.

What you claim don't know is whether the changes to the metaclass can affect
objects a and b after they have already been constructed.  Or, IOW, whether
the metaclass is "doubly dynamic" in the sense that changes to it not only
affect the class A at the time it is created, but also all the methods of A
when they are used.

Is the above description essentially right?   

If the metaclasses are "dynamic enough", wouldn't it be possible for it to
decide, at the time when its instances (ie classes) are created, to leave
hooks in their methods?  Such hooks would then enable aspects of these
method calls to be changed later.

Here's a newbie question to aspect oriented experts: is the set of aspects
that can be changed predefined?  If so, it seems to me that in principle
metaclass could do at least what aspect oriented approach could do.

I know little about either aspect oriented programming or the current status
of Python metaclasses, so I may be way off base here.

Huaiyu



More information about the Python-list mailing list