decorator to set attributes of function

前端 未结 3 1412
无人共我
无人共我 2020-12-09 19:58

I want different functions to be executable only if the logged in user has the required permission level.

To make my life more complexly simply I want to use decorat

3条回答
  •  囚心锁ツ
    2020-12-09 20:30

    Your decorator should return a function that can replace do_x or do_y , not return the execution result of do_x or do_y You can modity you decorate as below:

    def permission(permission_required):
        def wrapper(func):
            def inner():
                setattr(func, 'permission_required', permission_required)
                return func
            return inner()
        return wrapper
    

    Of course, you have another brief solution:

    def permission(permission_required):
        def wrapper(func):
            setattr(func, 'permission_required', permission_required)
            return func
        return wrapper
    

提交回复
热议问题