Preface

This article is a summary of my study in a teaching series , Introduction learning , There won't be much in-depth content .

Definition

The narrative in the course :

It's a higher order _ function _, The object used to decorate the function .

The content of self check may not be accurate enough :

Higher order function : Functions that handle functions
Decoration function : Add extra functionality to other functions

My own understanding is : Decorators can make a function have other functions .

Code example

# Decorator
def print_datetime(func):
#  One * Represents a variable parameter , Two ** Represents the keyword parameter , It means to pass it on here wrapper Parameters of ( Whatever it is ) Give them all again func() Transfer the past
    def wrapper(*args, **kw): 
        print(datetime.now())
        func(*args, **kw) 
    return wrapper
def hello(name):
    print('Hello, I'm {}'.format(name))
middle = print_datetime(hello)
middle('Sleepking')

The above code ,print_datetime() It's a decorator , The function is to let other functions output the current date . The last line of code will output a time and hello.i'am sleepking.

The above implementation uses variable parameters and keyword parameters in the function , I didn't know how to use these two concepts before , It's especially suitable for high-order functions ( To handle functions ).

In this case , Decorator ( function ) It actually returns a function , I feel like maybe all of them are basically like this , Turn the original function into a multi-functional function

A simpler way to call

# First define a decorator
def print_datetime(func):
def wrapper(*args, **kw): 
        print(datetime.now())
        func(*args, **kw) 
    return wrapper
# adopt @ The decorator name method adds the function of the decorator to the function defined later .
@print_datetime
def hello(name):
    print('Hello, I'm {}'.format(name))
hello('Tom') 
#  The call above is equivalent to using wrapper('Tom')

property Decorator

The course I'm looking at directly introduces property Decorator , But I personally feel that it's not easy for me to understand , I had to check some contents myself . Finally, I summarize what I think I need to know :

  • @property The decorator's function is to enable you to use the functions you define in your class by calling properties .
  • @property Itself creates another decorator ,@xxx.setter xxx For decorated functions , Properties are generally used to set , You can do anything , But the decorated function name here must be the same as @property The decorated function names are the same
  • Any function in the class can be decorated with this , In general, it's used for class properties get,set Function

Code example

class User:
    def __init__(self,name,email):
        self.name = name
        self.email = email
    def get_name(self):
        return self.name
    def set_name(self):
        self.name = name
    @property
    def get_email(self):
        return self.email
# You can use it on the top xx.get_email  call
    @property
    def print_word(self):
        print('ko you {}'.format(self.name))
    @print_word.setter
    def print_word(self,word):
        print('ko you {}'.format(word))
# Defined setter You can assign values to functions because ‘ Turn into ’ Property  print_word() = 'ok'

I didn't understand it at first , Finally found , The reason I don't understand is that I always think this decorator can only be used to operate on class properties and so on . But in fact, the function of this decorator is to call a method like a property , Because it's a similar property , So there will be one xxx.setter The other ornament of ( Properties can be assigned values and so on ).

reflection

It's because I'm at the entry level , I think it's a bit of a detour .

Function is originally to achieve a function , More functions are achieved with decorators , Unless there's a strong connection between these two functions , Otherwise, why not call two functions ? Maybe I didn't really understand the essence of this concept .

I think it's better to see all kinds of tutorials on the official website , But my English level is not good , So we have to give up .