How to get the name of decorator for function at run time?

Jayendra Parmar

In the following code the methods(print_a and print_b) of class Test, has been decorated by the two different decorators.

How can I determine that the given method(let say print_a), is decorated with some specific decorator (decorator1) at the runtime?

The easy solution is to change the name of the wrapper function, i.e. changing the wrapper to wrapper1 and wrapper2 in the decorator method, but the problem with that is I don't have control over that part of the code.

Does python have any reflection API like Java, and could that help me here ?

def my_decorator1(func):
    def wrapper(*args, **kwargs):
        print('I am decorated:1')
        func(*args, **kwargs)
    return wrapper

def my_decorator2(func):
    def wrapper(*args, **kwargs):
        print('I am decorated:2')
        func(*args, **kwargs)
    return wrapper

class Test():

    def __init__(self, a=None, b=None):
        self.a = a
        self.b = b

    @my_decorator1
    def print_a(self):
        print('Value of a is {}'.format(self.a))

    @my_decorator2
    def print_b(self):
        print('Value of b is {}'.format(self.b))


if __name__ == '__main__':

    d = Test.__dict__

    f1 = d.get('print_a')
    f2 = d.get('print_b')
mshroyer

Python decorators are more about transforming functions than about acting as simple metadata labels, as one often does with Java annotations. So to do this, I'd have the decorator set a property on the function it is wrapping (or perhaps wrap it in a callable of a particular type):

def my_decorator1(func):
    def wrapper(*args, **kwargs):
        print('I am decorated:1')
        func(*args, **kwargs)
    wrapper.decorator_name = 'my_decorator1'
    return wrapper

Then:

print(Test.print_a.decorator_name)

Alternatively, in Python 3.3+ you can use PEP 3155's __qualname__ to identify the decorator.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to get python file name in decorator function in inherited file

分類Dev

How can I get the parameter name in java at run time

分類Dev

How can I get the parameter name in java at run time

分類Dev

How to run "time" on a function in zsh

分類Dev

How to get function to run at specyfic time using python and PyQt not using Cron

分類Dev

How to get next run time Spring Scheduling?

分類Dev

How to get next run time Spring Scheduling?

分類Dev

Python decorator function called at compile time

分類Dev

How to get name of calling function/method in PHP?

分類Dev

How to get the Google Analytics global function name?

分類Dev

How to get caller function class name

分類Dev

How could i define a function at run-time in c

分類Dev

How to use a decorator to send arguments to a function in python

分類Dev

Nonlinear behavior of function run time

分類Dev

Improve function run-time

分類Dev

Get function name by address

分類Dev

How to get $scope or $element in an onblur event and run a function in an onblur event?

分類Dev

How do I get ng-options to run a function on select?

分類Dev

How to get the calling subclass' name in a superclass' static function?

分類Dev

How to use a numpy function as the loss function in PyTorch and avoid getting errors during run time?

分類Dev

How do i do run-time deploy ignite service of same name without restart the server node?

分類Dev

How to get compile time error checking with real valued function arguments?

分類Dev

How to run a separate function not included in if __name__=="__main__" from the command line?

分類Dev

Get calling function name in Clojure

分類Dev

How do inner decorator functions see decorated-function arguments?

分類Dev

My decorator change my function output, how to avoid this? Python

分類Dev

Change reference to function in run-time in Python

分類Dev

How can I get values from a TypeORM property decorator

分類Dev

How does the decorator @staticmethod in Python get rid of the instance object?

Related 関連記事

  1. 1

    How to get python file name in decorator function in inherited file

  2. 2

    How can I get the parameter name in java at run time

  3. 3

    How can I get the parameter name in java at run time

  4. 4

    How to run "time" on a function in zsh

  5. 5

    How to get function to run at specyfic time using python and PyQt not using Cron

  6. 6

    How to get next run time Spring Scheduling?

  7. 7

    How to get next run time Spring Scheduling?

  8. 8

    Python decorator function called at compile time

  9. 9

    How to get name of calling function/method in PHP?

  10. 10

    How to get the Google Analytics global function name?

  11. 11

    How to get caller function class name

  12. 12

    How could i define a function at run-time in c

  13. 13

    How to use a decorator to send arguments to a function in python

  14. 14

    Nonlinear behavior of function run time

  15. 15

    Improve function run-time

  16. 16

    Get function name by address

  17. 17

    How to get $scope or $element in an onblur event and run a function in an onblur event?

  18. 18

    How do I get ng-options to run a function on select?

  19. 19

    How to get the calling subclass' name in a superclass' static function?

  20. 20

    How to use a numpy function as the loss function in PyTorch and avoid getting errors during run time?

  21. 21

    How do i do run-time deploy ignite service of same name without restart the server node?

  22. 22

    How to get compile time error checking with real valued function arguments?

  23. 23

    How to run a separate function not included in if __name__=="__main__" from the command line?

  24. 24

    Get calling function name in Clojure

  25. 25

    How do inner decorator functions see decorated-function arguments?

  26. 26

    My decorator change my function output, how to avoid this? Python

  27. 27

    Change reference to function in run-time in Python

  28. 28

    How can I get values from a TypeORM property decorator

  29. 29

    How does the decorator @staticmethod in Python get rid of the instance object?

ホットタグ

アーカイブ