Python engineering code learning notes (updated at any time)

Keywords: Python

1. format usage of Python formatted output

"{replaced variable 0}... {replaced variable 1}...". Format (variable 0, variable 1) variables can be of any type

name = "Jack"
num = 1
print("{0}yes{1}A schoolbag".format(name,num))

2.assert function: judge whether the condition is true. If true, continue to run the program. If false, end the program and throw an exception AssertionError

Usage: assert expression [, parameter]     Judge whether the expression is true. If it is false, AssertionError will be thrown. The parameter is the parameter of AssertionError, for example:

a = 1
b = 2
assert a==b,"a Not equal to{}".format(b)

If a is not equal to b, AssertionError is thrown, and its parameters are: A is not equal to b (2), and the program is interrupted

Traceback (most recent call last):
  File "C:/Pycharm/PycharmProjects/Project_test_1/test.py", line 35, in <module>
    assert a==b,"a Not equal to{}".format(b)
AssertionError: a Not equal to 2

3.Python decorator and static decorator

When we define a class, the first parameter is always self, and the method of the class also has the parameter self. When we call the method obj.method through an instance, we pass the instance obj itself as a parameter to the method by default. For example

  In the class haha, the x method has the parameter self. When using obj.method, it will automatically input obj as a parameter into the x method. If we run the following code, the results are as follows:

 

When we instantiate C, we call c.x() automatically to pass the instance of C as a parameter to the X method. Therefore, it is equivalent to introducing a parameter.

Solution: add @ staticmethod before the method

  After adding a static decorator, method x is equivalent to an ordinary function. When using obj.method, instance c will not be passed into method x as a parameter. In addition to using static methods, you can also directly call methods with classes: directly access methods through classes, that is, haha.x(). In this way, instances will not be passed into method x as parameters.

Now let's talk about decorators:

Decorator in Python is used to decorate or decorate a defined function. For example, it allows you to do other things before and after the function runs. The essence of decorator is a second-order function.

First of all, in Python, all elements are regarded as objects or even functions. We can define new functions in other functions, return another function in other functions, or pass other functions as parameters to another function. Let's take a look at an example:

def func_1():
    print("You are in function 1")

def func_2(func_1):

    def func_3():
        print("You are in function 3")

    print("You are in function 2")
    func_1()
    return func_3

a = func_2(func_1)
print("Finished executing function 2")
print(a)
print(a())
Output is:
You are in function 2
 You are in function 1
 Finished executing function 2
<function func_2.<locals>.func_3 at 0x00000190459BF700>
You are in function 3

When executing function 2, output function 2 and function 1 in sequence, and then assign the address of the returned function 3 to A. We output a and find that the output is the address func_3 of the function. Only when outputting a(), it is equivalent to outputting func_3(), and the function content will be executed.

Let's use an example to illustrate the function of the decorator: let the function do some other things before and after execution

def func():
    print("Execution function")

def decorator(func):

    def func_1():
        print("What to do before executing the function")
        func()
        print("What to do after executing the function")

    return func_1

func()
func = decorator(func)
func()
Output is:
Execution function
 What to do before executing the function
 Execution function
 What to do after executing the function

We define a decorator decorator, and the parameter passed in is func. Inside the decorator, we define a new function func_1: do things before and after func is executed, and the decorator returns a new function. Before and after decorating, we can find that the output will change accordingly. In fact, after decorating, func has become func_1. The decorator must Return a new function, otherwise it cannot be given to func. Of course, the above code can be used in a more concise way:

def decorator(func):

    def func_1():
        print("What to do before executing the function")
        func()
        print("What to do after executing the function")

    return func_1

@decorator
def func():
    print("Execution function")

func()
Output:
What to do before executing the function
 Execution function
 What to do after executing the function

We added a @ decorator symbol in front of func to replace the statement: func = decorator(func). In this way, the output of func is directly the function modified by the decorator. This is the function of the decorator.

Posted by Gonwee on Sun, 10 Oct 2021 18:54:01 -0700