好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Python中高阶函数以及函数装饰器的解析

下面小编就为大家带来一篇详谈Python高阶函数与函数装饰器(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

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) 
c = lambda x:x+1 #x就是形参,c就是这个匿名函数的对象

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

calc(-5,10,abs) #引用上一节的实例,将-5和10绝对值相加 
#函数可以被赋值
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这个函数 
import requests #首先导入一个模块,这个可以不用记

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

g = get("http://HdhCmsTestbaidu测试数据") #调用:首先因为作用域的原因,我们无法访问到里层的wapper函数,所以我们直接调用get函数这里返回了一个wapper函数
print(g()) # 然后我在调用g(get函数)的对象,这样是不是就访问到里层的wapper函数呢 
#嵌套调用,在一个函数中调用另一个函数的功能
#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 
def geturl(url):
 response = requests.get(url)
 print(response.status_code)

geturl(http://HdhCmsTestbaidu测试数据) 
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://HdhCmsTestbaidu测试数据) 
#一个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('file')
def home():
 print("welcome")

home() #执行home-->wrapper 
a = 10
b = 20
c = 30

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

def read2():
 print("in the read2") 
import leonyan #Python IDE这行会爆红,但是不用管

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

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

print(leonyan.a + leonyan.b + leonyan.c ) # 
输出60
import leonyan as ly
import pandas as pd #这是一个第三方模块,以后的博客中会写到,这是一个用于做统计的 
from *** import ***

from leonyan import read1 #引入直接调用
read1() 
from leonyan import read1,read2 在同一行中可以引用多个,只需要用逗号隔开就行了

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

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

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

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

def fib(n): # write Fibonacci series up to n
 a, b = 0, 1
 while b < n:
 print(b, end=' ')
 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模块导入搜索路径

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

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

在实际的开发环境中,你不可能一个文件的代码写到底,当然你也有可能会引用同文件夹中的其他模块,但是你有没有想过这一个项目不可能是你一个写的,都是很多人协作开发。这样就存在这样的一个问题了;不同的人不可能用一台电脑,也不可能在一个文件夹下面写写功能。他们也有自己的代码文件夹,然后大家把功能通过接口的方式,提供调用。这时候就面临这不同文件夹的调用问题。而这种问题也需要通过from ** import ** 调用。

上图中我运行“模块导入.py”这个文件夹,首先我from Pythonscript.leonyan测试数据mand import config,因为我们得运行脚本和需要导入的包都在Pythonscript的目录下面所以我直接通过绝对路径导入,然后一层一层“.”下去,知道最后import了这个config文件,这里我们需要注意一点:当脚本在最外层运行的时候sys.path 列表中的第一个参数就是运行脚本的目录,这是什么意思,这代表着你在别的包中有调用了其他的东西比如说我的config.py是调用了bing.py文件,这时候就必须写绝对路径,因为这在sys.path文件夹中已经找不到了,也就是导入不进来。

总结: 包的导入其实都是很简单的,你需要记住一点:当你导入Python内建或者下载的第三方模块直接用import 导入,如果是自己写的就用from ** import ** 使用绝对目录导入就行了,也就是从调用脚本的上级目录开始导入。这样可以保证不会报模块导入的错误了。

以上就是Python中高阶函数以及函数装饰器的解析的详细内容,更多请关注Gxl网其它相关文章!

查看更多关于Python中高阶函数以及函数装饰器的解析的详细内容...

  阅读:40次