Home  >  Article  >  Backend Development  >  Analysis of high-order functions and function decorators in Python

Analysis of high-order functions and function decorators in Python

黄舟
黄舟Original
2017-10-01 07:44:271137browse

The following editor will bring you a detailed discussion of Python high-order functions and function decorators (recommended). The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.

1. Review of the previous section

Python2 and Python3 character encoding issues, whether you are a beginner or I already know Python projects well, and I will make some coding mistakes. I will briefly summarize the differences between Python3 and Python2 here.

First of all, Python3-->code files are all interpreted in utf-8. When the code and files are read into the memory, they become Unicode. This is why Python only encodes but not decodes, because the character encoding in the memory is changed to Unicode, and Unicode is a universal code that can be "translated" so the format encoding is format. In Python3, str and bytes are two formats, and bytes can be used as a binary representation.

Python2 uses the system's default character encoding to interpret the code, so if you want to use utf-8 to interpret the code, you must declare it in the header; and there is decoding and encoding in Python2, but the decoding action is necessary and the encoding action can Ignore, because the Python code loaded into the memory is Unicode, which is the same as python3; another thing to note in Python2 is that str and bytes have the same meaning. The str in Python2 is the bytes format in Python3, and the str in Python3 is actually Unicode.

Function basis (here I use the binary search in the recursive function)

Why use functions: The program will be designed as a module

There are three forms of defined functions:

- Parameterless function

- Function with parameters

- Empty function

PS: If the function has multiple return values, then the returned data format Is a tuple

-How to limit the parameter data format when the function passes in parameters.

def leon(x:int,y:int)->int:

pass

where x and y are specified here must be int type" -> ; "means that the function return value must also be of type int

print(yan.__annotations__): displays the limited data format of the formal parameters and the format of the return value


a = [1,2,3,4,5,7,9,10,11,12,14,15,16,17,19,21] #形参中的num
def calc(num,find_num):
 print(num)
 mid = int(len(num) / 2)   #中间数的下标
 if mid == 0: #递归函数非常重要的判断条件
 if num[mid] == find_num:
  print("find it %s"%find_num)
 else:
  print("cannt find num")
 if num[mid] == find_num: #直接找到不用递归,结束函数
 print("find_num %s"%find_num)
 elif num[mid] > find_num: #find_num应该在左边,向下递归
 calc(num[0:mid],find_num)

 elif num[mid] < find_num: #find_num应该在右边,向下递归
 calc(num[mid+1:],find_num)
calc(a,12)

Anonymous function


c = lambda x:x+1 #x就是形参,c就是这个匿名函数的对象

print(c(22))

Higher-order function-feature

1. Pass the memory address of one function to another function as a parameter

2. A function returns another function as a return value


def calc(a,b,c):
print(c(a) + c(b))

calc(-5,10,abs) #引用上一节的实例,将-5和10绝对值相加

2. High-level Function (supplement)

Function is a first-class object

Function can be assigned a value

can be used as a parameter

can be used as The return value

can be used as an element of the container type


#函数可以被赋值
def leon():
 print("in the leon")

l = leon
l()

#函数可以被当做参数
def yan(x): #这里x形参,其实就是我们调用实参的函数名
 x() #运行函数

y = yan(leon)


#函数当做返回值
def jian(x): 和上面一样这这也必须传入一个函数
 return x
j = jian(leon) #这里需要注意一点就是这里的意思是运行jian这个函数而这个函数返回的是x 也就是leon这个函数的内存地址,也就是说这时候leon这个函数并没有被执行
j() #运行 leon函数

#可以做为容器类型的元素
leon_dict = {"leon":leon}

leon_dict["leon"]() #这样也可以运行leon这个函数

3. Closure function

1.What is closure? Let me take a look and compare the concept on the official website (this is not what I found on the official website, but it doesn’t matter, you can’t understand it anyway):

Closure (Closure) is Lexical Closure (Lexical Closure) The abbreviation of is a function that refers to free variables. The referenced free variable will remain with the function even after it has left the environment in which it was created. Therefore, a closure is an entity composed of a function and its associated reference environment.

Confused? nonexistent. I will talk about it briefly below, but one thing is very important. Closure is the focus of decorators. If you don’t really understand closures, you will quickly forget them after learning decorators. Let’s illustrate it through an example


import requests #首先导入一个模块,这个可以不用记

def get(url): #定义一个get函数里面需要传一个url的位置参数
 def wapper(): #在定义一个wapper函数
 res = requests.get(url) #这一步就是打开一个网页
 return res.text #将网页以文字的形式返回
 return wapper #返回最里层的wapper函数

g = get("http://www.baidu.com") #调用:首先因为作用域的原因,我们无法访问到里层的wapper函数,所以我们直接调用get函数这里返回了一个wapper函数
print(g()) # 然后我在调用g(get函数)的对象,这样是不是就访问到里层的wapper函数呢

PS: Here we can treat the function as a special variable. When the code goes from top to bottom When executing, if the function is not called, the code within the function will not be executed. Take the above example. When we execute the get function, the memory address of the wapper function will be returned. However, the wapper function has not been executed at this time. In other words, the status returned by g() at this time is actually the wapper. , this means we only need to run g, which is equivalent to running the code in the wapper.

4. Nested calling of functions

Nested calling is actually easy to understand, it is the result of calling another function in one function , which is the return thing. Similarly, let’s take a look at a very simple code.


#嵌套调用,在一个函数中调用另一个函数的功能
#calc这个函数就是在对比两个数字的大小
def calc2(x,y):
 if x >y :
 return x
 else:
 return y

#我靠老板非常变态,然你直接计算四个数字的大小,擦。
def calc4(a,b,c,d):
 res1 = calc2(a,b) #res1的值,这里不就是calc2这个函数比较时最大的哪一个吗。
 res2 = calc2(res1,c)
 res3 = calc2(res2,d)
 return res3

With the above code we make a memory. When are nested calls used? Obviously, our function (calc4) needs the execution result of another function (return's y or x).

5. Decorator (advanced closure function)

Take the following code as an example. How to calculate the running time of the code without changing the source code


def geturl(url):
 response = requests.get(url)
 print(response.status_code)

geturl(http://www.baidu.com)


def timer(func):
 def wapper(url):
 start_time = time.time()
 func(url)
 stop_time = time.time()
 so_time_is = stop_time - start_time
 print("运行时间%s"%so_time_is)
 return wapper


@timer
def geturl(url):
 response = requests.get(url)
 print(response.status_code)

python = geturl(http://www.baidu.com)

Illustrated code

装饰器必备:

@timer就是装饰器,意思是装饰它下面的函数,而装饰器和被装饰的都是一个函数。

timer(装饰器函数),首先它会有一个位置参数(func)名字随意,但是必须并且只能是一个位置参数

func参数就是被装饰的geturl这个函数

为什么func是geturl这个函数呢-->上面写了一个装饰器功能:geturl=timer(geturl),我们看到这里的timer中传入的其实就是func函数所以func = geturl(被装饰的函数)

分析geturl=timer(geturl),首先我们可以得知timer这是一个闭包函数,当我们执行这个闭包函数,会把里层的函数(wapper)返回,也就是说timer(geturl)其实就是返回的wapper,所以就可以这样理解了geturl==wapper,所以当我们运行geturl的时候就相当于在执行wapper()这样的一个操作;如果这里实在记不住,就这样。咱上面不是有一个闭包函数吗?你就把geturl=timer(geturl)中的geturl(执行函数的返回结果)当做上面g(函数调用的返回结果),然后在分别再执行了下"g"或者"geturl”这个对象。

如果被装饰者有位置参数的话,我们需要在wapper函数中加上对应的位置参数用来接收,如果长度是不固定的话还可以用*args和**kwargs

六、有参装饰器

听着名字顾名思义,就是在装饰器中还有位置参数。


#一个low得不能再low得验证脚本,如果是显示环境中所有数据必须是由数据库或者一个静态文件提供,并且登录成功时,需要保存用户的一个状态

def auth(auth_type): #有参装饰器名称
 def auth_deco(func): #定义第二层函数名称
 def wrapper(*args,**kwargs): #最里层函数,主要实现认证功能
  if auth_type == "file":
  username = input("username>>:").strip()
  password = input("username>>").strip()
  if username == "leon" and password == "loveleon":
   res = func(*args,**kwargs)
   return res
  elif auth_type == "mysql_auth":
  print("mysql_auth...")
  return func(*args,**kwargs)
 return wrapper #第二层返回的是wrapper函数,其实就是home
 return auth_deco #第一层返回的结果等于第二层函数的名称

@auth(&#39;file&#39;)
def home():
 print("welcome")

home() #执行home-->wrapper

有参函数必备知识:

套路,通过上面无参装饰器,我们得出了geturl=timer(geturl)这个等式。回到有参装饰器,我们又会有什么样子的等式呢?首先@auth("file")是一个装饰器也就是一个函数,所以我们定义了一个auth(auth_type)这个函数,而这个函数返回的是什么呢?没有错就是第二层函数;到了这里我们就会发现@auth("file")其实就是@auth_deco,现在我们知道了现在装饰器其实就是auth_deco,那剩下的还不知道怎么写吗?

整理公式,auth('file')-----------(return)> auth_deco----->@auth_deco ->home=auth_deco(home)

如果记不住?如果实在是记不住,其实就可以这样理解,有参装饰器无非就是在无参装饰器上面加了一层(三层),然后在第一层返回了第二层的函数,而到了第二层就和我们普通用的装饰器是一毛一样了

七、模块导入

import ,创建一个leonyan.py的模块文件,等待被导入


a = 10
b = 20
c = 30

def read1():
 print("in the read1")

def read2():
 print("in the read2")

导入leonyan.py文件(调用模块文件和模块文件在同一目录下)


import leonyan #Python IDE这行会爆红,但是不用管

leonyan.read1() #执行leonyan这个包中的read1函数

leonyan.read2() #执行leonyan这个包中read2函数

print(leonyan.a + leonyan.b + leonyan.c ) #输出60

总结:在Python中包的导入(import ***)会干三个事情:1:创建新的作用域;2:执行该作用域的顶级代码,比如你导入的那个包中有print执行后就会直接在屏幕中输出print的内容;3:得到一个模块名,绑定到该模块内的代码

在模块导入的时候给模块起别名


import leonyan as ly
import pandas as pd #这是一个第三方模块,以后的博客中会写到,这是一个用于做统计的

 

给模块起别名还是挺多的,在有些模块的官方文档中,还是比较推荐这种方法的,比如pandas的官方文档中就是起了一个pd别名,总之as就是一个模块起别名


from *** import ***

from leonyan import read1 #引入直接调用
read1()

 

如果在调用模块的函数作用域中有相同的同名的,会将调用过来的覆盖。

在form ** import ** 中控制需要引用的变量(函数其实在未被执行的时候也是一个存放在内存中的变量)


from leonyan import read1,read2 在同一行中可以引用多个,只需要用逗号隔开就行了

print(read1)
print(read2)
#这里打印的就是read1和read2的内存地址

#需求我现在只需要导入read2

这时候我们就可以在leonyan这个函数中加上这么一行:

__all__ = ["read2"] #这里的意思就是别的文件调用为的时候用from ** import ** 只能拿到read2 这个函数的内存地址,也就是只有read2可以被调用

把模块当做一个脚本执行

我们可以通过模块的全局变量__name__来查看模块名:

当做脚本运行:

__name__ 等于'__main__'

作用:用来控制.py文件在不同的应用场景下执行不同的逻辑

if __name__ == '__main__':


#fib.py

def fib(n): # write Fibonacci series up to n
 a, b = 0, 1
 while b < n:
 print(b, end=&#39; &#39;)
 a, b = b, a+b
 print()

def fib2(n): # return Fibonacci series up to n
 result = []
 a, b = 0, 1
 while b < n:
 result.append(b)
 a, b = b, a+b
 return result

if __name__ == "__main__":
 import sys
 fib(int(sys.argv[1]))

代码执行 Python flb.py 100

只需要简单了解的Python模块导入搜索路径

内建(build-in)  --> sys.path(sys.path是一个列表,而且第一个位置就是当前文件夹)

模块导入的重点-->包的导入

In an actual development environment, you cannot write the code of a file to the end. Of course, you may also reference other modules in the same folder, but have you ever thought that this project cannot be written by you alone? , are developed collaboratively by many people. This creates a problem; it is impossible for different people to use the same computer, and it is impossible to write functions under the same folder. They also have their own code folders, and then everyone calls the functions through interfaces. At this time, we face the problem of calling these different folders. This kind of problem also needs to be called from ** import **.

In the picture above, I run the "module import.py" folder. First, I import config from Pythonscript.leonyan.command, because we have to run the script and the things that need to be imported. The packages are all under the Pythonscript directory, so I imported them directly through the absolute path, and then went down layer by layer with "." until I finally imported the config file. Here we need to pay attention to one thing: when the script is running in the outermost layer, sys. The first parameter in the path list is the directory where the script is run. What does this mean? It means that you have called other things in other packages. For example, my config.py calls the bing.py file. This You must write the absolute path because it can no longer be found in the sys.path folder, which means it cannot be imported.

Summary: The import of packages is actually very simple. You need to remember one thing: when you import Python built-in or downloaded third-party modules, use import import, if you write it yourself, just use from ** import ** and use the absolute directory to import, that is, import from the upper-level directory of the calling script. This ensures that module import errors will not be reported.

The above is the detailed content of Analysis of high-order functions and function decorators in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn