AOP use cases
Michele Simionato
michele.simionato at poste.it
Thu Apr 22 10:19:13 EDT 2004
Peter Hansen <peter at engcorp.com> wrote in message news:<XYmdnVLB9rjbARrdRVn-vg at powergate.ca>...
> Could
> you please provide an example from your own uses which demonstrates
> the effectiveness of AOP versus "not AOP" in the same way that the
> synchronization example posted earlier is clearly better when done
> as a wrapper than with the code duplicated everywhere (i.e. with
> the "concerns" not separated)?
>
> -Peter
I think Jacek made a beautiful post, or maybe he just summarized the way I
see AOP: nothing really different for "regular" programming, just another
way to help separation of concerns. Since you (Peter) always ask for
examples, here is a little script I posted few days ago on the mailing list,
which solves the issue of separating the concern of checking the
arguments of a constructor from the definition of the constructor.
==== quoting from a previous post of mine =====
class _WithConstructorChecked(type): # helper metaclass
def __call__(cls, *args, **kw):
assert len(args)<=2, "%s called with more than 2 args" % cls
assert kw.has_key("kw"), "%s needs a 'kw=' argument" % cls
return super(_WithConstructorChecked,cls).__call__(*args,**kw)
class WithConstructorChecked(object): # mixin class
__metaclass__ = _WithConstructorChecked
class C(WithConstructorChecked):
def __init__(self, *args, **kw):
pass
c=C(1,2,kw=3) # ok; try different signatures to get assertion errors
In this example the mixin class WithConstructorChecked ensures that C is
called with at least two positional arguments and a keyword argument named
'kw'. The code of class C is not touched at all, you just add
WithConstructorChecked to the list of its bases.
=== end quote ===
I tend to avoid the usage of AOP techniques in my production code, since I feel
them too magical and most of the time I can do what I want in "regular"
Python. Personally, I hate the hype about AOP, but I do like the concept.
Michele Simionato
More information about the Python-list
mailing list