Python decorator? - can someone please explain this?

前端 未结 6 957
离开以前
离开以前 2020-11-29 10:42

Apologies this is a very broad question.

The code below is a fragment of something found on the web. The key thing I am interested in is the line beginning @protect

6条回答
  •  遥遥无期
    2020-11-29 11:28

    Decorator is just a function that takes another function as an argument

    Simple Example:

    def get_function_name_dec(func):
      def wrapper(*arg):
          function_returns = func(*arg)  # What our function returns
          return func.__name__ + ": " + function_returns
    
      return wrapper
    
    @get_function_name_dec
    def hello_world():
        return "Hi"
    
    print(hello_world())
    

提交回复
热议问题