Auto-register class methods using decorator

Posted by adamk on Stack Overflow See other posts from Stack Overflow or by adamk
Published on 2010-06-16T14:47:49Z Indexed on 2010/06/16 14:52 UTC
Read the original article Hit count: 163

Filed under:

I want to be able to create a python decorator that automatically "registers" class methods in a global repository (with some properties).

Example code:

class my_class(object):

    @register(prop1,prop2)
    def my_method( arg1,arg2 ):
       # method code here...

    @register(prop3,prop4)
    def my_other_method( arg1,arg2 ):
       # method code here...

I want that when loading is done, somewhere there will be a dict containing:

{ "my_class.my_method"       : ( prop1, prop2 )
  "my_class.my_other_method" : ( prop3, prop4 ) }

Is this possible?

© Stack Overflow or respective owner

Related posts about python