自学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-装饰器举例的更多相关文章

  1. 自学Python4.4-装饰器的进阶

    自学Python之路-Python基础+模块+面向对象自学Python之路-Python网络编程自学Python之路-Python并发编程+数据库+前端自学Python之路-django 自学Pyth ...

  2. 自学Python4.2-装饰器

    自学Python之路-Python基础+模块+面向对象自学Python之路-Python网络编程自学Python之路-Python并发编程+数据库+前端自学Python之路-django 自学Pyth ...

  3. 自学Python4.3-装饰器固定格式

    自学Python之路-Python基础+模块+面向对象自学Python之路-Python网络编程自学Python之路-Python并发编程+数据库+前端自学Python之路-django 自学Pyth ...

  4. Python装饰器举例分析

    概述 装饰器本质上是一个Python函数,它可以让其他函数在不需要做任何代码变动的前提下增加额外功能,装饰器的返回值也是一个函数对象. 我们要需要一个能测试函数运行时间的decorator,可以定义如 ...

  5. 自学Python4.6-迭代器

    自学Python之路-Python基础+模块+面向对象自学Python之路-Python网络编程自学Python之路-Python并发编程+数据库+前端自学Python之路-django 自学Pyth ...

  6. python 扩展注册功能装饰器举例

    db_path='db.txt'def get_uname(): while True: uname=input('请输入用户名:').strip() if uname.isalpha(): with ...

  7. python--4、装饰器

    装饰器(Decorator) 使用场景:为被装饰器装饰的函数增加功能,但又不希望修改函数的定义,即在代码运行期间动态增加功能. 装饰器更多的用于后期功能升级而不是编写新的代码.装饰器不光能装饰函数,也 ...

  8. 初学 Python(十五)——装饰器

    初学 Python(十五)--装饰器 初学 Python,主要整理一些学习到的知识点,这次是生成器. #-*- coding:utf-8 -*- import functools def curren ...

  9. python-生成器、迭代器、装饰器

    目录 动态语言和静态语言 __slots__ 生成器 迭代器 闭包 装饰器 动态语言和静态语言 动态语言可以在运行的过程中修改代码,例如python在运行的过程中给已创建好的类添加属性和方法. 静态语 ...

随机推荐

  1. Maven安装与环境配置(Windows)

    1.下载安装包 在Maven官网下载最新版的安装包:http://maven.apache.org/download.cgi 2.解压安装包 3.配置Maven环境变量 配置M2_HOME环境变量,指 ...

  2. Mysql 字符集及排序规则

    一.字符集 字符集:就是用来定义字符在数据库中的编码的集合. 常见的字符集:utf8.Unicode.GBK.GB2312(支持中文).ASCCI(不支持中文)   二.字符集排序规则   作者本人用 ...

  3. RDD

    scala> val rdd1=sc.parallelize(Array("coffe","coffe","hellp"," ...

  4. Mapper动态代理方式

    开发规范 Mapper接口开发方法只需要程序员编写Mapper接口(相当于Dao接口),由Mybatis框架根据接口定义创建接口的动态代理对象,代理对象的方法体同Dao接口实现类方法. Mapper接 ...

  5. Stream、FileStream、MemoryStream的区别

    1.Stream:流,在msdn的定义:提供字节序列的一般性视图,Stream提供了读写流的方法是以字节的形式从流中读取内容.而我们经常会用到从字节流中读取文本或者写入文本,微软提供了StreamRe ...

  6. vue axios 封装(三)

    封装三: import axios from 'axios' import { Message, MessageBox } from 'element-ui' import store from '. ...

  7. How to helloworld on Xcode

    create a mac app,click button to change label text? create project,click left window button on Main. ...

  8. Spring Boot 构建电商基础秒杀项目 (八) 商品创建

    SpringBoot构建电商基础秒杀项目 学习笔记 新建数据表 create table if not exists item ( id int not null auto_increment, ti ...

  9. JSON in SQL Server 2016

    JSON functions in SQL Server enable you to analyze and query JSON data, transform JSON to relational ...

  10. codeforces347B

    Fixed Points CodeForces - 347B A permutation of length n is an integer sequence such that each integ ...