一 函数对象

一 函数是第一类对象,即函数可以当作数据传递

  1. #1 可以被引用
  2. #2 可以当作参数传递
  3. #3 返回值可以是函数
  4. #3 可以当作容器类型的元素

二 利用该特性,优雅的取代多分支的if

  1. def foo():
  2. print('foo')
  3.  
  4. def bar():
  5. print('bar')
  6.  
  7. dic={
  8. 'foo':foo,
  9. 'bar':bar,
  10. }
  11. while True:
  12. choice=input('>>: ').strip()
  13. if choice in dic:
  14. dic[choice]()

二 函数嵌套

一 函数的嵌套调用

  1. def max(x,y):
  2. return x if x > y else y
  3.  
  4. def max4(a,b,c,d):
  5. res1=max(a,b)
  6. res2=max(res1,c)
  7. res3=max(res2,d)
  8. return res3
  9. print(max4(1,2,3,4))

二 函数的嵌套定义

  1. def f1():
  2. def f2():
  3. def f3():
  4. print('from f3')
  5. f3()
  6. f2()
  7.  
  8. f1()
  9. f3() #报错,为何?请看下一小节

三 名称空间与作用域

一 什么是名称空间?

  1. #名称空间:存放名字的地方,三种名称空间,(之前遗留的问题x=1,1存放于内存中,那名字x存放在哪里呢?名称空间正是存放名字x与1绑定关系的地方)

二 名称空间的加载顺序

  1. python test.py
  2. #1、python解释器先启动,因而首先加载的是:内置名称空间
  3. #2、执行test.py文件,然后以文件为基础,加载全局名称空间
  4. #3、在执行文件的过程中如果调用函数,则临时产生局部名称空间

三 名字的查找顺序

  1. 局部名称空间--->全局名称空间--->内置名称空间
  2.  
  3. #需要注意的是:在全局无法查看局部的,在局部可以查看全局的,如下示例
  4.  
  5. # max=1
  6. def f1():
  7. # max=2
  8. def f2():
  9. # max=3
  10. print(max)
  11. f2()
  12. f1()
  13. print(max) 

四 作用域

  1. #1、作用域即范围
  2. - 全局范围(内置名称空间与全局名称空间属于该范围):全局存活,全局有效
  3.   - 局部范围(局部名称空间属于该范围):临时存活,局部有效
  4. #2、作用域关系是在函数定义阶段就已经固定的,与函数的调用位置无关,如下
  5. x=1
  6. def f1():
  7. def f2():
  8. print(x)
  9. return f2
  10. x=100
  11. def f3(func):
  12. x=2
  13. func()
  14. x=10000
  15. f3(f1())
  16.  
  17. #3、查看作用域:globals(),locals()
  18.  
  19. LEGB 代表名字查找顺序: locals -> enclosing function -> globals -> __builtins__
  20. locals 是函数内的名字空间,包括局部变量和形参
  21. enclosing 外部嵌套函数的名字空间(闭包中常见)
  22. globals 全局变量,函数定义所在模块的名字空间
  23. builtins 内置模块的名字空间

五 global与nonlocal关键字

四 闭包函数

一 什么是闭包?

  1. #内部函数包含对外部作用域而非全局作用域的引用
  2.  
  3. #提示:之前我们都是通过参数将外部的值传给函数,闭包提供了另外一种思路,包起来喽,包起呦,包起来哇
  4.  
  5. def counter():
  6. n=0
  7. def incr():
  8. nonlocal n
  9. x=n
  10. n+=1
  11. return x
  12. return incr
  13.  
  14. c=counter()
  15. print(c())
  16. print(c())
  17. print(c())
  18. print(c.__closure__[0].cell_contents) #查看闭包的元素

二 闭包的意义与应用

  1. #闭包的意义:返回的函数对象,不仅仅是一个函数对象,在该函数外还包裹了一层作用域,这使得,该函数无论在何处调用,优先使用自己外层包裹的作用域
  2. #应用领域:延迟计算(原来我们是传参,现在我们是包起来)
  3. from urllib.request import urlopen
  4.  
  5. def index(url):
  6. def get():
  7. return urlopen(url).read()
  8. return get
  9.  
  10. baidu=index('http://www.baidu.com')
  11. print(baidu().decode('utf-8'))

五 装饰器

https://www.cnblogs.com/moyand/p/8673022.html

装饰器就是闭包函数的一种应用场景

一 为何要用装饰器

  1. #开放封闭原则:对修改封闭,对扩展开放

二 什么是装饰器

  1. 装饰器他人的器具,本身可以是任意可调用对象,被装饰者也可以是任意可调用对象。
  2. 强调装饰器的原则:1 不修改被装饰对象的源代码 2 不修改被装饰对象的调用方式
  3. 装饰器的目标:在遵循12的前提下,为被装饰对象添加上新功能

三 装饰器的使用

  1. import time
  2. def timmer(func):
  3. def wrapper(*args,**kwargs):
  4. start_time=time.time()
  5. res=func(*args,**kwargs)
  6. stop_time=time.time()
  7. print('run time is %s' %(stop_time-start_time))
  8. return res
  9. return wrapper
  10.  
  11. @timmer
  12. def foo():
  13. time.sleep(3)
  14. print('from foo')
  15. foo()

无参装饰器

  1. def auth(driver='file'):
  2. def auth2(func):
  3. def wrapper(*args,**kwargs):
  4. name=input("user: ")
  5. pwd=input("pwd: ")
  6.  
  7. if driver == 'file':
  8. if name == 'egon' and pwd == '':
  9. print('login successful')
  10. res=func(*args,**kwargs)
  11. return res
  12. elif driver == 'ldap':
  13. print('ldap')
  14. return wrapper
  15. return auth2
  16.  
  17. @auth(driver='file')
  18. def foo(name):
  19. print(name)
  20.  
  21. foo('egon')

有参装饰器

四 装饰器语法

  1. 被装饰函数的正上方,单独一行
  2. @deco1
  3. @deco2
  4. @deco3
  5. def foo():
  6. pass
  7.  
  8. foo=deco1(deco2(deco3(foo)))

五 装饰器补充:wraps

  1. from functools import wraps
  2.  
  3. def deco(func):
  4. @wraps(func) #加在最内层函数正上方
  5. def wrapper(*args,**kwargs):
  6. return func(*args,**kwargs)
  7. return wrapper
  8.  
  9. @deco
  10. def index():
  11. '''哈哈哈哈'''
  12. print('from index')
  13.  
  14. print(index.__doc__)

六 叠加多个装饰器

  1. # 叠加多个装饰器
  2. # 1. 加载顺序(outter函数的调用顺序):自下而上
  3. # 2. 执行顺序(wrapper函数的执行顺序):自上而下
  1. def outter1(func1): #func1=wrapper2的内存地址
  2. print('加载了outter1')
  3. def wrapper1(*args,**kwargs):
  4. print('执行了wrapper1')
  5. res1=func1(*args,**kwargs)
  6. return res1
  7. return wrapper1
  8.  
  9. def outter2(func2): #func2=wrapper3的内存地址
  10. print('加载了outter2')
  11. def wrapper2(*args,**kwargs):
  12. print('执行了wrapper2')
  13. res2=func2(*args,**kwargs)
  14. return res2
  15. return wrapper2
  16.  
  17. def outter3(func3): # func3=最原始的那个index的内存地址
  18. print('加载了outter3')
  19. def wrapper3(*args,**kwargs):
  20. print('执行了wrapper3')
  21. res3=func3(*args,**kwargs)
  22. return res3
  23. return wrapper3
  24.  
  25. @outter1 # outter1(wrapper2的内存地址)======>index=wrapper1的内存地址
  26. @outter2 # outter2(wrapper3的内存地址)======>wrapper2的内存地址
  27. @outter3 # outter3(最原始的那个index的内存地址)===>wrapper3的内存地址
  28. def index():
  29. print('from index')
  30.  
  31. print('======================================================')
  32. index()

示范代码

六 练习题

一:编写函数,(函数执行的时间是随机的)
二:编写装饰器,为函数加上统计时间的功能
三:编写装饰器,为函数加上认证的功能

四:编写装饰器,为多个函数加上认证的功能(用户的账号密码来源于文件),要求登录成功一次,后续的函数都无需再输入用户名和密码
注意:从文件中读出字符串形式的字典,可以用eval('{"name":"egon","password":"123"}')转成字典格式

五:编写装饰器,为多个函数加上认证功能,要求登录成功一次,在超时时间内无需重复登录,超过了超时时间,则必须重新登录

六:编写下载网页内容的函数,要求功能是:用户传入一个url,函数返回下载页面的结果

七:为题目五编写装饰器,实现缓存网页内容的功能:
具体:实现下载的页面存放于文件中,如果文件内有值(文件大小不为0),就优先从文件中读取网页内容,否则,就去下载,然后存到文件中

扩展功能:用户可以选择缓存介质/缓存引擎,针对不同的url,缓存到不同的文件中

八:还记得我们用函数对象的概念,制作一个函数字典的操作吗,来来来,我们有更高大上的做法,在文件开头声明一个空字典,然后在每个函数前加上装饰器,完成自动添加到字典的操作

九 编写日志装饰器,实现功能如:一旦函数f1执行,则将消息2017-07-21 11:12:11 f1 run写入到日志文件中,日志文件路径可以指定
注意:时间格式的获取
import time
time.strftime('%Y-%m-%d %X')

  1. #题目一:略
  2. #题目二:略
  3. #题目三:略
  4. #题目四:
  5. db='db.txt'
  6. login_status={'user':None,'status':False}
  7. def auth(auth_type='file'):
  8. def auth2(func):
  9. def wrapper(*args,**kwargs):
  10. if login_status['user'] and login_status['status']:
  11. return func(*args,**kwargs)
  12. if auth_type == 'file':
  13. with open(db,encoding='utf-8') as f:
  14. dic=eval(f.read())
  15. name=input('username: ').strip()
  16. password=input('password: ').strip()
  17. if name in dic and password == dic[name]:
  18. login_status['user']=name
  19. login_status['status']=True
  20. res=func(*args,**kwargs)
  21. return res
  22. else:
  23. print('username or password error')
  24. elif auth_type == 'sql':
  25. pass
  26. else:
  27. pass
  28. return wrapper
  29. return auth2
  30.  
  31. @auth()
  32. def index():
  33. print('index')
  34.  
  35. @auth(auth_type='file')
  36. def home(name):
  37. print('welcome %s to home' %name)
  38.  
  39. # index()
  40. # home('egon')
  41.  
  42. #题目五
  43. import time,random
  44. user={'user':None,'login_time':None,'timeout':0.000003,}
  45.  
  46. def timmer(func):
  47. def wrapper(*args,**kwargs):
  48. s1=time.time()
  49. res=func(*args,**kwargs)
  50. s2=time.time()
  51. print('%s' %(s2-s1))
  52. return res
  53. return wrapper
  54.  
  55. def auth(func):
  56. def wrapper(*args,**kwargs):
  57. if user['user']:
  58. timeout=time.time()-user['login_time']
  59. if timeout < user['timeout']:
  60. return func(*args,**kwargs)
  61. name=input('name>>: ').strip()
  62. password=input('password>>: ').strip()
  63. if name == 'egon' and password == '':
  64. user['user']=name
  65. user['login_time']=time.time()
  66. res=func(*args,**kwargs)
  67. return res
  68. return wrapper
  69.  
  70. @auth
  71. def index():
  72. time.sleep(random.randrange(3))
  73. print('welcome to index')
  74.  
  75. @auth
  76. def home(name):
  77. time.sleep(random.randrange(3))
  78. print('welcome %s to home ' %name)
  79.  
  80. index()
  81. home('egon')
  82.  
  83. #题目六:略
  84. #题目七:简单版本
  85. import requests
  86. import os
  87. cache_file='cache.txt'
  88. def make_cache(func):
  89. def wrapper(*args,**kwargs):
  90. if not os.path.exists(cache_file):
  91. with open(cache_file,'w'):pass
  92.  
  93. if os.path.getsize(cache_file):
  94. with open(cache_file,'r',encoding='utf-8') as f:
  95. res=f.read()
  96. else:
  97. res=func(*args,**kwargs)
  98. with open(cache_file,'w',encoding='utf-8') as f:
  99. f.write(res)
  100. return res
  101. return wrapper
  102.  
  103. @make_cache
  104. def get(url):
  105. return requests.get(url).text
  106.  
  107. # res=get('https://www.python.org')
  108.  
  109. # print(res)
  110.  
  111. #题目七:扩展版本
  112. import requests,os,hashlib
  113. engine_settings={
  114. 'file':{'dirname':'./db'},
  115. 'mysql':{
  116. 'host':'127.0.0.1',
  117. 'port':3306,
  118. 'user':'root',
  119. 'password':''},
  120. 'redis':{
  121. 'host':'127.0.0.1',
  122. 'port':6379,
  123. 'user':'root',
  124. 'password':''},
  125. }
  126.  
  127. def make_cache(engine='file'):
  128. if engine not in engine_settings:
  129. raise TypeError('egine not valid')
  130. def deco(func):
  131. def wrapper(url):
  132. if engine == 'file':
  133. m=hashlib.md5(url.encode('utf-8'))
  134. cache_filename=m.hexdigest()
  135. cache_filepath=r'%s/%s' %(engine_settings['file']['dirname'],cache_filename)
  136.  
  137. if os.path.exists(cache_filepath) and os.path.getsize(cache_filepath):
  138. return open(cache_filepath,encoding='utf-8').read()
  139.  
  140. res=func(url)
  141. with open(cache_filepath,'w',encoding='utf-8') as f:
  142. f.write(res)
  143. return res
  144. elif engine == 'mysql':
  145. pass
  146. elif engine == 'redis':
  147. pass
  148. else:
  149. pass
  150.  
  151. return wrapper
  152. return deco
  153.  
  154. @make_cache(engine='file')
  155. def get(url):
  156. return requests.get(url).text
  157.  
  158. # print(get('https://www.python.org'))
  159. print(get('https://www.baidu.com'))
  160.  
  161. #题目八
  162. route_dic={}
  163.  
  164. def make_route(name):
  165. def deco(func):
  166. route_dic[name]=func
  167. return deco
  168. @make_route('select')
  169. def func1():
  170. print('select')
  171.  
  172. @make_route('insert')
  173. def func2():
  174. print('insert')
  175.  
  176. @make_route('update')
  177. def func3():
  178. print('update')
  179.  
  180. @make_route('delete')
  181. def func4():
  182. print('delete')
  183.  
  184. print(route_dic)
  185.  
  186. #题目九
  187. import time
  188. import os
  189.  
  190. def logger(logfile):
  191. def deco(func):
  192. if not os.path.exists(logfile):
  193. with open(logfile,'w'):pass
  194.  
  195. def wrapper(*args,**kwargs):
  196. res=func(*args,**kwargs)
  197. with open(logfile,'a',encoding='utf-8') as f:
  198. f.write('%s %s run\n' %(time.strftime('%Y-%m-%d %X'),func.__name__))
  199. return res
  200. return wrapper
  201. return deco
  202.  
  203. @logger(logfile='aaaaaaaaaaaaaaaaaaaaa.log')
  204. def index():
  205. print('index')
  206.  
  207. index()

python之旅:函数对象、函数嵌套、名称空间与作用域、装饰器的更多相关文章

  1. Python 函数对象-函数嵌套-名称空间与作用域-闭包函数

    今日内容: 1. 函数对象 函数是第一类对象: 指的是函数名指向的值可以被当中数据去使用 1.可以被引用 2.可以当做参数传给另一个函数 3.可以当做一个函数的返回值 4.可以当做容器类型的元素 2. ...

  2. 【0809 | Day 12】可变长参数/函数的对象/函数的嵌套/名称空间与作用域

    可变长参数 一.形参 位置形参 默认形参 二.实参 位置实参 关键字实参 三.可变长参数之* def func(name,pwd,*args): print('name:',name,'pwd:',p ...

  3. day12 函数对象,名称空间与作用域

    """ 今日内容: 1.函数参数传值细节 2.三元表达式 3.列表及字典推导式 4.函数对象 5.名称空间与作用域 6.函数的嵌套定义初识 ""&qu ...

  4. day11-函数对象、名称空间和作用域

    目录 函数对象 函数的嵌套 名称空间和作用域 内置名称空间 全局名称空间 局部名称空间 作用域 全局作用域 局部作用域 global和nonlocal 函数对象 在Python中,一切皆对象,函数也是 ...

  5. Python 名称空间和作用域

    a = 10 # lst = [1,2,3,4] # # # 内置函数 print("你好啊,我叫赛利亚") # def chi(): a = 10 b = 20 # # # # ...

  6. python全栈开发-Day9 函数对象、函数嵌套、名称空间与作用域

    一 .函数对象 一 .函数是第一类对象,即函数可以当作数据传递 可以被引用 可以当作参数传递 返回值可以是函数 可以当作容器类型的元素 二. 利用该特性,优雅的取代多分支的if def foo(): ...

  7. Python入门之函数的嵌套/名称空间/作用域/函数对象/闭包函数

    本篇目录: 一.函数嵌套 二.函数名称空间与作用域 三.函数对象 四.闭包函数 ============================================================ ...

  8. python第十二天, 三元表达式, 函数对象,名称空间与作用域,函数的嵌套定义

    复习 1. 字符串的比较: 2. 函数的参数:形参与实参 3. 实参的分类:位置实参与关键字实参 4. 形参分类: 1.无值位置形参 2.有值位置形参 3.可变长位置形参 4.有无值关键字形参 5.可 ...

  9. Python——day11 函数(对象、名称空间、作用域、嵌套、闭包)

    一.函数对象  函数名就是存放了函数的内存地址,存放了内存地址的变量都是对象,即 函数名 就是 函数对象  函数对象的应用 1. 可以直接被引用  fn = cp_fn 2 .可以当作函数参数传递 c ...

随机推荐

  1. sar命令详解

    基础命令学习目录首页 原文链接:http://lovesoo.org/linux-sar-command-detailed.html sar(System Activity Reporter系统活动情 ...

  2. Oracle purge 用法介绍

    http://blog.csdn.net/indexman/article/details/27379597

  3. Scrum Meeting 11 -2014.11.17

    今天和其他两个小组讨论了关于整合问题,在数据库连接等具体方面上还需要继续商讨. 我们小组内部讨论了,这周还是需要在处理整合的同时做项目整体的测试与改进的. Member Today’s task Ne ...

  4. Java第一次试验

    北京电子科技学院(BESTI) 实     验    报     告 课程:Java程序设计   班级:1352       姓名:朱国庆   学号:20135237 成绩:             ...

  5. 实验一linux 系统简介和实验二基本概念及操作

    作业 zy e

  6. python实现树莓派开机自动发送IP到指定邮箱

    #!/usr/bin/python # -*- coding:UTF-8 -*- #测试发送邮件163邮箱发送到qq邮箱 import smtplib from email.mime.text imp ...

  7. 第二次作业<1>

    1001.A+B Format (20) ac代码 1. 解题思路 先求和,再输出. 答案区间为-2,000,000至2,000,000,将答案分为三个区段分类讨论.虽然觉得很烦但是想不出更好的方法. ...

  8. EGener2四则运算出题器

    项目源码: https://git.coding.net/beijl695/EGener2.git (代码纯属原创,设计细节不同,请思量) 项目发布后,由于期间各种事情,耽搁至最后一天交付.这次的项目 ...

  9. 001_JavaWeb之Servlet的路径映射问题

    001_JavaWeb之Servlet的路径映射问题 在web.xml中写入: <servlet> <servlet-name>DeleteStudent</servle ...

  10. mac下搭建MySql环境

    准备工作做完后,开始: 创建数据库 step1: 在mac中->系统偏好设置->最下边点MySQL 在弹出页面中 关闭mysql服务(点击stop mysql server) step2: ...