AOP decorator?

Jon Clements joncle at googlemail.com
Mon Mar 1 14:49:39 EST 2010


On Mar 1, 4:22 pm, gentlestone <tibor.b... at hotmail.com> wrote:
> Hi,
>
> suppose my source code looks like:
> --------------------------------
>   import aspect_xy
>   class Basic(object, aspect_xy.Basic):
>     pass # basic attributes and methods ...
> --------------------------------
> 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?

Untested and written quickly, so all possible disclaimers apply ;)

class BaseObject(object):
    pass

def aspect_xy_dec(base):
    def wrapper(cls):
	return type(cls.__name__, (base, getattr(aspect_xy, cls.__name__)),
{})
    return wrapper

@aspect_xy(BaseObject)
class SomeObject:
    pass


Cheers,

Jon.





More information about the Python-list mailing list