This is the 30th day of my participation in the Wenwen Challenge

1. An overview of the

Decorators refer to existing functions with the @ symbol, which can be used to decorate other functions.

When A program decorates another function (such as B) with an “@ function” (such as function A), there are two steps:

  1. Pass the decorated function (function B) as an argument to the function referenced by the @ sign (function A)
  2. Replaces (decorates) function B with the return value of step 1

Finally, the function decorated with the “@ function” is no longer the original function, but is replaced with something new.

Decorators are divided into built-in decorators and custom decorators.

  • @classmethod, the method that you’re modifying is a classmethod
  • @staticmethod, which is a staticmethod
  • @property, which turns a method call into a property call
  • Custom decorators that modify other functions by creating custom decorators based on the function

For details on Python’s built-in decorators, see Python’s article on class memory analysis

Let’s go~

2. Built-in decorators

2.1 @ classmethod

Class methods are methods that belong to class objects. Class methods are defined by the @classMethod decorator

Instance attributes and s cannot be used in class methods

@classmethod def classmethod name (CLS,[parameter list]): function bodyCopy the code

Take 🌰 to see what works

Class Animal: zone = "Asia" # def __init__(self,name): self. Name = name @classmethod def zoneinfo(CLS): Print (cls.zone) cat = Animal("Tome") cat. Zoneinfo ()Copy the code

2.2 @ staticmethod

Python allows you to define methods that are independent of class objects, called static methods.

Static methods are no different from defining ordinary functions in modules

But static methods put into the namespace of the class need to be “class calls”, need to create objects, do not implicitly pass self

Static methods are defined by the @staticMethod decorator, of the following format:

@staticmethod def staticmethod name ([parameter list]): function bodyCopy the code

Take 🌰 to see what works

Class Animal: zone = "Asia" # def __init__(self,name): self. Name = name @staticmethod def food(food1,food2): Print (" Favorite food: {0},{1}".format(food1,food2)) # call Animal(" Apple","Orange") cat = Animal("Tome") cat. Food ("Apple","Orange")Copy the code

2.3 @ propety

@property can turn a method call into a property call. Usually used instead of gettar and settar methods, easy to operate

@property def Function name (): function bodyCopy the code
class Student: def __init__(self,name,score): self.name = name self.__score = score @property def score(self): Print ("Loading....") ) return self.__score @score.setter def score(self,score): if -1 < score < 101: self.__score = score else: Print (" score ") print(" score ") print(" score ") print(" score ")Copy the code

3. Customize decorators

We can define custom functions as decorators and use @ to decorate other functions as “precursors” to execute functions.

  • A decorator is essentially a Python function that allows other functions to add extra functionality without making any code changes, and the return value of the decorator is also a function object
  • Decorators can be used in log insertion, performance testing, transaction processing, caching, and permission verification.
  • Pull out a lot of the same code that has nothing to do with the function itself and continue to reuse it

There are four common forms of decorators:

  1. No parameter decorator
  2. Decorator with parameters
  3. Decorator with variable parameters
  4. Class-based implementation decorators

We’re going to take a look at decorators, and then we’re going to look at how to create custom decorators

Let’s take a look at 🌰 to see how to implement a simple decorator insert log print and experience an autodefine decorator

Def Mylogging(func): def wrapper(info): print("function {func}()".format(func=func.__name__)) print(" start executing.." ) return func(info) return wrapper @Mylogging def test(info): Print ("{0}".format(info)) print("{0}".format(info))Copy the code

conclusion

Python has three built-in decorators (@classMethod, @StaticMethod, and @Property). You can also customize the decorators to decorate other functions as required.

A function decorated by a decorator is always replaced with the return value of the function referenced by the @ symbol. If the return value of the function referenced by the @ symbol is a function, then the decorated function is still a function after the substitution.

In a nutshell, decorators are just some of the pre-actions that we do before we use our test cases.

Ok, this is the content of this issue, welcome everyone to comment in the comments section, like follow, see you next time ~