Aspect Programming Module
Yermat
loic at fejoz.net
Fri Apr 16 05:38:42 EDT 2004
Alexandre Fayolle wrote:
> Le 16-04-2004, Yermat <loic at fejoz.net> a écrit :
>
>
>>Does it help ?
>
>
> Well, yes and no. I agree that this is how AOP can be made to work in
> Python (aand your implementation is not conceptually very different from
> logilab.aspect (except that the module has some entry points for
> choosing which methods are tied to the aspect)
>
> However, I would not call this a "mixin" (which was the key word in my
> question). My vision of a mixin implies (maybe wrongly) some
> responsibilities to be added or customized through multiple inheritance
> to a given class by a mixin class. This supposes that the originial
> class was designed with the mixin in mind.
>
Oups sorry !
So maybe something like the following.
In fact, I'm really wondering what is really AOP. It seems just like
code factorization after all and use it only on some instance !
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 FunctionLoggerClass(object):
def __getattribute__(self, attrName):
realAttr = object.__getattribute__(self, attrName)
if callable(realAttr):
return LoggerAOP(realAttr, attrName)
return realAttr
class MyClass(object):
def __init__(self, name):
self.name = name
def foo(self, a):
return "%s say hello to %s" % (self.name, a)
class SpecMyClass(FunctionLoggerClass, MyClass):
pass
m = SpecMyClass("Yermat")
print m.name
print m.foo("world")
--
Yermat
More information about the Python-list
mailing list