Access to static members from inside a method decorator?

Maric Michaud maric at aristote.info
Thu Oct 5 11:44:15 EDT 2006


Le jeudi 05 octobre 2006 17:18, glen.coates.bigworld at gmail.com a écrit :
> I guess my solution is slightly less elegant because
> it requires this ugly explicit init call outside the classes that it
> actually deals with, however it is more efficient because the dir()
> pass happens once on module load, instead of every time I want the list
> of exposed methods.

You can always replace the need of the init method on classes using a 
metaclass.

This demonstrates it with Bruno's expose decorator, but it can be done with 
your actual init func too.

In [6]: class m(type) :
   ...:     def __init__(self, *a,**kw) :
   ...:         for name, meth in self.__dict__.items() :
   ...:             if getattr(meth, '_exposed', False) :
   ...:                 print 'exposed :', name
   ...:
   ...:

In [7]: class a(object):
   ...:     __metaclass__ = m
   ...:     def f(self) :pass
   ...:     @expose
   ...:     def g(self) :pass
   ...:
   ...:
exposed : g

In [8]: class b(a) :
   ...:     @expose
   ...:     def h(self) :pass
   ...:
   ...:
exposed : h




-- 
_____________

Maric Michaud
_____________

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097



More information about the Python-list mailing list