AOP decorator?
Terry Reedy
tjreedy at udel.edu
Mon Mar 1 14:41:27 EST 2010
On 3/1/2010 11:22 AM, gentlestone wrote:
> Hi,
>
> suppose my source code looks like:
> --------------------------------
> import aspect_xy
> class Basic(object, aspect_xy.Basic):
> pass # basic attributes and methods ...
As a sidenote, this violates my understanding of aspect-oriented
programming, which is to add aspects that go across several derived
classes. But...
> --------------------------------
> and the source code of aspect_xy.py is:
> --------------------------------
> class Basic(object):
> pass # aspect extra attributes and methods ...
> --------------------------------
> how can I write this with decorators? I want something like:
> -------------------------------
> import aspect_xy
> @aspect_xy # extra attributes and methods ...
> class Basic(object):
> pass # basic attributes and methods ...
> ----------------------------------------------------------
> I want to write class decorator function, which:
> 1. Takes the basic class as parameter
> 2. Find the same class name in aspect_xy.py
> 3. Inherit the found aspect class
> Is it possible?
You would have to look at the difference between Basic with and without
the explicit base class and patch the latter to look like the former. At
minimum, you would have to fix .__bases__ and .__mro__, but I do not
know if those are writable. To me, it seems easier to be explicit.
tjr
More information about the Python-list
mailing list