Aspect Programming Module

Yermat loic at fejoz.net
Fri Apr 16 03:31:50 EDT 2004


Alexandre Fayolle wrote:
> Le 15-04-2004, Max M <maxm at mxm.dk> a écrit :
> 
>>Hung Jung Lu wrote:
>>
>>
>>>Python does not even have codeblocks. So how can you say AOP is not
>>>needed for Python programmers?
>>
>>I am probably rather dense, but I have not seen aspect oriented examples 
>>  that could not have been done with simple mixins.
> 
> 
> Hi Max,
> 
> I'm probably rather dense too :-) 
> How do you implement the logging aspect with a simple mixin?
> 
> (the logging aspect prints a statement before and after each method call
> of a class instance)
> 

Does it help ?

class LoggerAOP(object):
     def __init__(self, f, name):
         self.f = f
         self.name = name

     def __call__(self, *args, **keywords):
         if callable(self.f):
             print "calling %s" % self.name
             result = self.f(*args, **keywords)
             print "end of call %s" % self.name
             return result
         raise TypeError, "%s not callable" % self.name

class MyClass(object):
     def __init__(self, name):
         self.name = name

     def foo(self, a):
         return "%s say hello to %s" % (self.name, a)

m = MyClass("Yermat")
print 'm.name:', m.name
print 'm.foo:', m.foo("world")

m.foo = LoggerAOP(m.foo, 'foo')

print 'm.name:', m.name
print 'm.foo:', m.foo("world")

-- 
Yermat




More information about the Python-list mailing list