From then to the bubbling to recursively decorator

  • Recursive algorithm to resolve
  • Bubble sort resolved
  • Decorator resolve

A recursive

  1. recursive definition   

    Recursion (Recursion), has become recursive, in mathematics and computer science, it refers to the use of the method in the definition of the function itself functions. Recursive further longer term used to describe a similar procedure is repeated from the process of things.

The Fibonacci column is a typical recursive case: 
F0 = 0 (initial value) 
Fl = 1 (initial value) 
n-all integers greater than 1: F (n) = F ( n - 1) + F (n - 2 ) (recursive definition) 
Although there are many recursive mathematical functions can be expressed, but in practical applications, recursion overhead is too large, it is best not to use recursion is not necessary under the circumstances.

  2. The principle of recursive

    . (1) Example:

def recursion(defth, a1, a2):
    if defth == 5:
        return a1
    a3 = a1 + a2
    defth = defth + 1
    print(a1)
    a1 = recursion(defth, a2, a3)
    return a1
ret = recursion(1, 0, 1)
print(ret)

 

 

  The following picture shows the painting during the execution of the entire function, the red represents the nested layers of fill, the return value of the return of the outside layers of green representing a function. In fact, recursion is the principle, by performing a function of the flow entering this function again, after returning a value by a condition, layer by layer in accordance with the flow of execution just returned back again, and finally get the return value, but when recursion to note two points:

  1. his condition, he must make recursive can return a value within a certain condition, otherwise it will have been recursively until the computer resource depletion (Python default recursive limited number of times)

  2. Return value, in which the general recursive function to give him some return value, otherwise the last time will not return recursion desired value.

 

  

II. Bubble Sort

  1. Bubble Sort principle

    Bubble sort is a simple sorting algorithm. He repeated visits over the number of columns to be sorted, a comparison of two elements, if they put them in the wrong order switching over.

Operation of bubble sort algorithm is as follows: 
  1. Comparison of adjacent elements. If the first is greater than the second, the two of them exchanged. 
  2. do the same work for each pair of adjacent elements, from the beginning to the end of the first to the last pair. Once this is done, the last element is the biggest number. 
  3. Repeat the above steps for all elements, except the last one. 
  4. Repeat the above steps every time duration of fewer and fewer elements, a pair of numbers until there is no need to compare

    Data exchange principle 

    By means of a peripheral to save the original variable value, then go to exchange data by pointing to the address conversion. Note: When the temp points to a, b and a point to the time point temp itself is not changed, it is in the following figure, a point to b, but still points to a temp address, so he was 66

a = 66
b = 88
temp = a 
a = b 
b = temp 

  

    Bubble sort of principle

  

 

 2. bubble sort case

 1 # -*- coding:utf-8 -*-
 2 # zhou
 3 # 2017/6/17
 4 list = [0, 88, 99, 33, 22, 11, 1]
 5 for j in range(1, len(list)):
 6     for i in range(len(list) - j):
 7         # 如果第一个数据大, 则交换数据, 否则, 不做改变
 8         if list[i] > list[i + 1]:
 9             temp = list[i]
10             list[i] = list[i + 1]
11             list[i + 1] = temp
12 print(list)

三. 装饰器

  1. 装饰器定义

     装饰器是什么呢?简单来说就是在不改变源函数代码的基础上,对代码的一种微妙的扩展,以使得其功能进一步增强的函数。装饰器就是一个函数,一个加载在其他函数之上的函数,

    以下我们先来了解几个概念:

    <1>.  函数执行流

      由以上面的可知,函数的执行流应该是从上到下的,也就是说,代码先把第一个test1加载到内存中,然后从新开辟一块内存来存放第二个test1。

      这里应该是第一个test1的指向改到了890673481792这里。

def test1():
    print('日本人.')
print(id(test1))
def test1():
    print('中国人.')
print(id(test1))
test1()

执行结果:
890673481656
890673481792
中国人.

    <2>.  函数作为变量

      由以下结果可以看出来,函数名其实就是一个变量,可以用来传递的变量。

      注意:函数作为变量的时候不能加括号,必须只是函数名

def test1():
    print('我是.')
a = test1
print(a)
print(test1)

结果: <function test1 at 0x000000E2D403C7B8> <function test1 at 0x000000E2D403C7B8>

    <3>.  函数嵌套

      这里定义了三个函数,test1,test2,test3, 3里面嵌套了1, 1里面又嵌套了2,从他的结果中想必你也会看出来函数的执行流。

def test1():
    print('我是',end='')
    def test2():
        print('中国人.')
    test2()

def test3():
    test1()
    print('Hello, China.')
test3()

结果:
我是中国人.
Hello, China.

  2. 装饰器原理

    (1). 装饰器的写法和使用

      <1>. 装饰器也是一个函数

      <2>. 使用装饰器的格式: 在一个函数前面加上:@装饰器的名字

    (2). 装饰器的原理

      <1>. 把test1函数当做一个变量传入outer中

          func = test1

      <2>. 把装饰器嵌套的一个函数inner赋值给test1

          test1 = inner

      <3>. 当执行test1函数的时候,就等于执行了inner函数,因此在最后的那个test1()命令其实执行的就是inner,因此先输出(你是哪国人)

      <4>. 按照执行流执行到func函数的时候,其实执行的就是原来的test1函数,因此接着输出(我是中国人),并把它的返回值返回给了ret

      <5>. 当原来的test1函数执行完了之后,继续执行inner里面的命令,因此输出了(Oh,hh, I love China.)

    (3). 装饰器的总结

      由上面的执行流可以看出来,其实装饰器把之前的函数当做参数传递进去,然后创建了另一个函数用来在原来的函数之前或者之后加上所需要的功能。

# -*- coding:utf-8 -*-
# zhou
# 2017/6/17
# 定义了一个装饰器outer
def outer(func): def inner(): print('你是哪国人?') ret = func() print('Oh, hh, I love China.') return inner @outer def test1(): print('我是中国人.') test1()

 

  3. 带参数的装饰器

    为了装饰器的高可用,一般都会采用下面的方式,也就是无论所用的函数是多少个参数,这个装饰器都可以使用

    Python内部会自动的分配他的参数。

# -*- coding:utf-8 -*-
# zhou
# 2017/6/17
def outer(func):
    def inner(a, *args, **kwargs):
        print('你是哪国人?')
        ret = func(a, *args, **kwargs)
        print('Oh, hh, I love China.')
    return inner

@outer
def test1(a, *args, **kwargs):
    print('我是中国人.')

test1(1)

  3.  装饰器的嵌套

   <1>. 第一层装饰器的简化(outer装饰器)

    

    

    <2>. 第二层装饰器简化(outer0装饰器)

    

    <3>. 装饰器嵌套攻击额,我们可以发现一层装饰器其实就是把原函数嵌套进另一个函数中间,因此我们只需要一层一层的剥开嵌套就可以了。

# -*- coding:utf-8 -*-
# zhou
# 2017/6/17
def outer0(func):
    def inner():
        print('Hello, Kitty.')
        ret = func()
        print('我是日本人.')
    return inner

def outer(func):
    def inner():
        print('你是哪国人?')
        ret = func()
        print('你呢?')
    return inner
@outer0
@outer
def test1():
    print('我是中国人.')

test1()

结果Hello, Kitty.
你是哪国人?
我是中国人.
你呢?
我是日本人.

Guess you like

Origin www.cnblogs.com/ltn26/p/10981375.html