Auto-register class methods using decorator

前端 未结 7 915
粉色の甜心
粉色の甜心 2020-12-05 00:46

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

Example code:

         


        
7条回答
  •  萌比男神i
    2020-12-05 01:16

    Here's a little love for class decorators. I think the syntax is slightly simpler than that required for metaclasses.

    def class_register(cls):
        cls._propdict = {}
        for methodname in dir(cls):
            method = getattr(cls, methodname)
            if hasattr(method, '_prop'):
                cls._propdict.update(
                    {cls.__name__ + '.' + methodname: method._prop})
        return cls
    
    
    def register(*args):
        def wrapper(func):
            func._prop = args
            return func
        return wrapper
    
    
    @class_register
    class MyClass(object):
    
        @register('prop1', 'prop2')
        def my_method(self, arg1, arg2):
            pass
    
        @register('prop3', 'prop4')
        def my_other_method(self, arg1, arg2):
            pass
    
    myclass = MyClass()
    print(myclass._propdict)
    # {'MyClass.my_other_method': ('prop3', 'prop4'), 'MyClass.my_method': ('prop1', 'prop2')}
    

提交回复
热议问题