自学Python4.5-装饰器举例
自学Python之路-Python基础+模块+面向对象
自学Python之路-Python网络编程自学Python之路-Python并发编程+数据库+前端自学Python之路-django
自学Python4.5 - 装饰器举例
举例1. 编写装饰器,为多个函数加上认证的功能(用户的账号密码来源于文件)
要求登录成功一次,后续的函数都无需再输入用户名和密码
FLAG = False
def login(func):
def inner(*args,**kwargs):
global FLAG
'''登录程序'''
if FLAG:
ret = func(*args, **kwargs) # func是被装饰的函数
return ret
else:
username = input('username : ')
password = input('password : ')
if username == 'carlos' and password == 'carlos88':
FLAG = True
ret = func(*args,**kwargs) #func是被装饰的函数
return ret
else:
print('登录失败')
return inner @login
def shoplist_add():
print('增加一件物品') @login
def shoplist_del():
print('删除一件物品') shoplist_add()
shoplist_del()

举例2. 编写装饰器,为多个函数加上记录调用功能,要求每次调用函数都将被调用的函数名称写入文件
def log(func):
def inner(*args,**kwargs):
with open('log','a',encoding='utf-8') as f:
f.write(func.__name__+'\n')
ret = func(*args,**kwargs)
return ret
return inner @log
def shoplist_add():
print('增加一件物品') @log
def shoplist_del():
print('删除一件物品') shoplist_add()
shoplist_del()
shoplist_del()
shoplist_del()
shoplist_del()
shoplist_del()
3. 编写下载网页内容的函数,要求功能是:用户传入一个url,函数返回下载页面的结果
为题目1编写装饰器,实现缓存网页内容的功能:
具体:实现下载的页面存放于文件中,如果文件内有值(文件大小不为0),就优先从文件中读取网页内容,否则,就去下载,然后存到文件中
import os
from urllib.request import urlopen
def cache(func):
def inner(*args,**kwargs):
if os.path.getsize('web_cache'):
with open('web_cache','rb') as f:
return f.read()
ret = func(*args,**kwargs) #get()
with open('web_cache','wb') as f:
f.write(b'*********'+ret)
return ret
return inner @cache
def get(url):
code = urlopen(url).read()
return code # {'网址':"文件名"}
ret = get('http://www.baidu.com')
print(ret)
ret = get('http://www.baidu.com')
print(ret)
ret = get('http://www.baidu.com')
print(ret)
自学Python4.5-装饰器举例的更多相关文章
- 自学Python4.4-装饰器的进阶
自学Python之路-Python基础+模块+面向对象自学Python之路-Python网络编程自学Python之路-Python并发编程+数据库+前端自学Python之路-django 自学Pyth ...
- 自学Python4.2-装饰器
自学Python之路-Python基础+模块+面向对象自学Python之路-Python网络编程自学Python之路-Python并发编程+数据库+前端自学Python之路-django 自学Pyth ...
- 自学Python4.3-装饰器固定格式
自学Python之路-Python基础+模块+面向对象自学Python之路-Python网络编程自学Python之路-Python并发编程+数据库+前端自学Python之路-django 自学Pyth ...
- Python装饰器举例分析
概述 装饰器本质上是一个Python函数,它可以让其他函数在不需要做任何代码变动的前提下增加额外功能,装饰器的返回值也是一个函数对象. 我们要需要一个能测试函数运行时间的decorator,可以定义如 ...
- 自学Python4.6-迭代器
自学Python之路-Python基础+模块+面向对象自学Python之路-Python网络编程自学Python之路-Python并发编程+数据库+前端自学Python之路-django 自学Pyth ...
- python 扩展注册功能装饰器举例
db_path='db.txt'def get_uname(): while True: uname=input('请输入用户名:').strip() if uname.isalpha(): with ...
- python--4、装饰器
装饰器(Decorator) 使用场景:为被装饰器装饰的函数增加功能,但又不希望修改函数的定义,即在代码运行期间动态增加功能. 装饰器更多的用于后期功能升级而不是编写新的代码.装饰器不光能装饰函数,也 ...
- 初学 Python(十五)——装饰器
初学 Python(十五)--装饰器 初学 Python,主要整理一些学习到的知识点,这次是生成器. #-*- coding:utf-8 -*- import functools def curren ...
- python-生成器、迭代器、装饰器
目录 动态语言和静态语言 __slots__ 生成器 迭代器 闭包 装饰器 动态语言和静态语言 动态语言可以在运行的过程中修改代码,例如python在运行的过程中给已创建好的类添加属性和方法. 静态语 ...
随机推荐
- 学习yii2.0——依赖注入
依赖注入 依赖注入是一种设计模式,可以搜索“php依赖注入”,这里不阐述了. yii框架的依赖注入 Yii 通过 yii\di\Container 类提供 DI 容器特性. 它支持如下几种类型的依赖注 ...
- Linux kernel support docker storage driver aufs
How to make docker use aufs in CentOS 7? - Server Faulthttps://serverfault.com/questions/650208/how- ...
- nginx强制使用https访问(http跳转到https)
Nginx 的 Location 从零开始配置 - 市民 - SegmentFault 思否https://segmentfault.com/a/1190000009651161 nginx配置loc ...
- Windows10 Build 18298 桌面显示计算机(此电脑)
- laravel log改为时间格式
1 providers新建文件 LogRotateServiceProvider.php <?php namespace App\Providers; use Monolog\Formatter ...
- Oracle 备份表数据
--备份表数据 select * from t_owners; --创建备份表 create table t_owners_copy ( id number, name ), addressid nu ...
- 缓存session,cookie,sessionStorage,localStorage的区别
https://www.cnblogs.com/cencenyue/p/7604651.html(copy) 浅谈session,cookie,sessionStorage,localStorage的 ...
- leetcode资料整理
注:借鉴了 http://m.blog.csdn.net/blog/lsg32/18712353 在Github上提供leetcode有: 1.https://github.com/soulmachi ...
- linux中$1的意思
$1 在shell中成为“位置参数”,表示传入的第一个参数.在shell脚本主体中,表示shell脚本的第一个参数.用在shell脚本函数里时,表示的是函数的第一个入参.
- 重写TreeView模板来实现数据分层展示(一)
总想花些时间来好好总结一下TreeView这个WPF控件,今天来通过下面的这几个例子来好好总结一下这个控件,首先来看看一个常规的带虚线的TreeView控件吧,在介绍具体如何完成之前首先来看看最终实现 ...