Registry of Methods via Decorators

Stephan Diehl stephan.diehl at gmx.net
Thu Jun 22 10:52:53 EDT 2006


bayerj schrieb:
> I want to make a registry of methods of a class during creation. My
> attempt was this
> 
> """ classdecorators.py
> 
> Author: Justin Bayer
> Creation Date: 2006-06-22
> Copyright (c) 2006 Chess Pattern Soft,
> All rights reserved.  """
> 
> class decorated(object):
> 
>     methods = []
> 
>     @classmethod
>     def collect_methods(cls, method):
>         cls.methods.append(method.__name__)
>         return method
> 
> class dec2(decorated):
> 
>     @collect_methods
>     def first_func(self):
>         pass
> 
>     @collect_methods
>     def second_func(self):
>         pass

replace '@collect_methods' with '@decorated.collect_methods'
and this will do what you want.

But keep in mind, that the 'methods' list in decorated will be used for 
all derived classes.



More information about the Python-list mailing list