Python recursive function (Fibonacci sequence), coroutines, function design specifications

One recursive:

  In the process of running, call yourself, call yourself layer by layer until you do not meet the conditions to exit

  [Put two mirrors on the opposite side, and then look at yourself in the mirror, you will find that there will be many layers of objects in the mirror]

  Recursion requires boundary conditions, can not loop indefinitely, recursive forward segment and recursive return segment;

For example: 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1

It can be understood as: 10 * (10-1) * ((10-1) -1) [Each call is decremented by 1]

A function contains a call to itself

def fact(n):
    if n<=1:
        return 1
    else:
        return n * fact(n-1)#fact(n)代表自身
test = fact ( 3 ) #Here is equivalent to fact (3) = 3 * fact (2) = 3 * 2 * fact (1) = 3 * 2 * 1
print(test)

Fibonacci sequence : recursive functions can also be used

1, 1, 2, 3, 5, 8, 13, 21, 34, ..., n = 1 and n = 2 all output 1

def fact(n):
    if n<=2:
        return 1
    else:
        return fact(n-1)+fact(n-2)
test=fact(34)
print(test)

 

Introduction of Second Coroutine

Coroutine: Concurrency under a single thread, also known as micro-thread, fiber. English name Coroutine. One sentence explains what a coroutine is: a coroutine is a lightweight thread in user mode, that is, a coroutine is controlled and scheduled by the user program itself.

What needs to be emphasized is:

  1. Python threads belong to the kernel level , that is, the scheduling is controlled by the operating system (such as a single thread encountering io or execution time is too long, it will be forced to surrender cpu execution authority and switch other threads to run)

  2. Open the coroutine in a single thread, once it encounters io, it will control the switch from the application level (not the operating system) to improve efficiency (!!!!!! Switching of non-io operations has nothing to do with efficiency)

Compared with the operating system controlling the switching of threads, users control the switching of coroutines in a single thread

advantage:

1. 协程的切换开销更小,属于程序级别的切换,操作系统完全感知不到,因而更加轻量级
2. 单线程内就可以实现并发的效果,最大限度地利用cpu

Disadvantages:

1. 协程的本质是单线程下,无法利用多核,可以是一个程序开启多个进程,每个进程内开启多个线程,每个线程内开启协程
2. 协程指的是单个线程,因而一旦协程出现阻塞,将会阻塞整个线程

Summarize the characteristics of coroutines:

  1. Must be concurrent in only one single thread
  2. No lock needed to modify shared data
  3. Save multiple contexts of control flow in the user program
  4. Additional: A coroutine automatically switches to other coroutines when it encounters IO operations (how to achieve detection of IO, yield and greenlet can not be achieved, the gevent module (select mechanism) is used)

 

Three, function design specifications:

  Coupling:

    (1) As much as possible to accept input through parameters and produce output through return to ensure the independence of the function;

    (2) Try to reduce global variables to communicate between functions;

    (3) Do not directly modify the variable type parameters in the function;

    (4) Avoid directly changing the variables defined in another module;

  Polymerization:

    (1) Each function should have a single, unified goal;

    (2) The function of each function should be relatively simple;

 

Guess you like

Origin www.cnblogs.com/xh0203/p/12682093.html