本节重点:

  • 掌握装饰器相关知识

python装饰器就是用于拓展原来函数功能的一种函数,这个函数的特殊之处在于它的返回值也是一个函数,使用python装饰器的好处就是在不用更改原函数的代码前提下给函数增加新的功能。

装饰器扩展登录功能

import json

def auth_user(username, password):
user_dict = json.load(open("file/user1.txt", "r", encoding="gbk"))
if username in user_dict:
if password == user_dict[username]:
return True
return False login_status = False def login(fun):
def inner(*args, **kwargs):
global login_status
if not login_status:
username = input("用户名:").strip()
password = input("密码:").strip()
if auth_user(username, password):
login_status = True
else:
print("wrong username or password")
if login_status:
fun(*args, **kwargs) return inner @login
def dalu():
print(" 欢迎来到大陆电影 ".center(30, "-")) @login
def hongkong():
print(" 欢迎来到香港电影 ".center(30, "-")) @login
def rihan():
print(" 欢迎来到日韩电影 ".center(30, "-")) @login
def oumei():
print(" 欢迎来到欧美电影 ".center(30, "-")) action_dict = {
1: dalu,
2: hongkong,
3: rihan,
4: oumei
} if __name__ == '__main__':
while True:
choice = input("""--- 选择功能 ---
1.大陆电影
2.香港电影
3.日韩电影
4.欧美电影
choice:""").strip()
if choice.isdigit() and int(choice) in action_dict:
action_dict[int(choice)]()

可选择登录方式的装饰器(带参数的装饰器)

import json

def auth_user(auth_type, username, password):
auth_data = json.load(open("file/user2.txt", "r", encoding="gbk"))
user_list = auth_data[auth_type]
if username in user_list:
if password == user_list[username]:
return True
return False login_status = False def login(auth_type):
def auth(fun):
def inner(*args, **kwargs):
global login_status
nonlocal auth_type
if not login_status:
username = input("用户名:").strip()
password = input("密码:").strip()
if auth_user(auth_type, username=username, password=password):
login_status = True
else:
print("wrong username or password")
if login_status:
fun(*args, **kwargs)
return inner
return auth # @login("qq")
def dalu():
print(" 欢迎来到大陆电影 ".center(30, "-")) # @login("wechat")
def hongkong():
print(" 欢迎来到香港电影 ".center(30, "-")) # @login("qq")
def rihan():
print(" 欢迎来到日韩电影 ".center(30, "-")) # @login("qq")
def oumei():
print(" 欢迎来到欧美电影 ".center(30, "-")) action_dict = {
1: dalu,
2: hongkong,
3: rihan,
4: oumei
} if __name__ == '__main__':
while True:
choice = input("""--- 选择功能 ---
1.大陆电影
2.香港电影
3.日韩电影
4.欧美电影
choice:""").strip()
if choice.isdigit() and int(choice) in action_dict:
auth_type = None
if not login_status:
auth_type = input("登录方式").strip()
auth = login(auth_type)
inner = auth(action_dict[int(choice)])
inner()

python学习之路 六 :装饰器的更多相关文章

  1. Python学习之路7☞装饰器

    一:命名空间与作用域 1.1命名空间 局部命名空间: def foo(): x=1 def func(): pass 全局命名空间: import time class ClassName:pass ...

  2. Python学习之路6 - 装饰器

    装饰器 定义:本质是函数,(装饰其他函数)就是为其他函数添加附加功能.原则:1.不能修改被装饰的函数的源代码 2.不能修改被装饰的函数的调用方式 实现装饰器的知识储备: 1.函数即“变量” 2.高阶函 ...

  3. Python成长之路_装饰器

    一.初入装饰器 1.首先呢我们有这么一段代码,这段代码假设是N个业务部门的函数 def f1(aaa): print('我是F1业务') if aaa == 'f1': return 'ok' def ...

  4. python学习日记(函数--装饰器)

    楔子 前提,我有一段代码(一个函数). import time def run_time(): time.sleep(0.1) print('我曾踏足山巅') 需求1:现在,我想计算这段代码的运行时间 ...

  5. 【Python学习之二】装饰器

    装饰器 首先,给出装饰器的框架: def log(func): def wrapper(*args, **kw): print('call %s():' % func.__name__) return ...

  6. python 学习笔记7(装饰器)

    闭包(closure)是函数式编程的重要的语法结构. 定义:如果在一个内部函数里,对在外部作用域(但不是在全局作用域)的变量进行引用,那么内部函数就被认为是闭包(closure). def outer ...

  7. Python 学习笔记9(装饰器,decorator)

    31 装饰器 装饰器可以对一个函数.方法或者类进行加工,是一种高级的python语法. 装饰函数 接收一个可调用对象作为输入参数,并返回一个新的可调用对象. 把函数传递给装饰器,然后增加新的功能,返回 ...

  8. python学习之路-day4-装饰器&json&pickle

    本节内容 迭代器&生成器 装饰器 Json & pickle 数据序列化 一.生成器 1.列表生成式 >>> L = [x * x for x in range(10 ...

  9. Python学习笔记九:装饰器,生成器,迭代器

    装饰器 本质是函数,装饰其他函数,为其他函数添加附加功能 原则: 1不修改原函数的源代码 2不修改原函数的调用方式 知识储备: 1函数即变量 使用门牌号的例子说明函数,调用方式与变量一致 2高阶函数 ...

随机推荐

  1. 搭建一个Web API项目(DDD)

    传送阵:写在最后 一.创建一个能跑的起来的Web API项目 1.建一个空的 ASP.NET Web应用 (为什么不直接添加一个Web API项目呢,那样会有些多余的内容(如js.css.Areas等 ...

  2. 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 14—Dimensionality Reduction 降维

    Lecture 14 Dimensionality Reduction 降维 14.1 降维的动机一:数据压缩 Data Compression 现在讨论第二种无监督学习问题:降维. 降维的一个作用是 ...

  3. linux下connect超时时间探究

    最近在linux做服务器开发的时候,发现了一个现象:服务器在启动的时候调用了 connect 函数,因为连接了一个不可用的端口,导致connect最后报出了 “Connection timed out ...

  4. IDEA快捷键【收藏】

    Ctrl+Alt+L 格式化代码Ctrl+Shift+J 两行合成一行,删去不必要的空格匹配代码格式其他快捷键:[常规]Ctrl+Shift + Enter,语句完成“!”,否定完成,输入表达式时按 ...

  5. java算法 第七届 蓝桥杯B组(题+答案) 7.剪邮票

    7.剪邮票  (结果填空) 如[图1.jpg], 有12张连在一起的12生肖的邮票.现在你要从中剪下5张来,要求必须是连着的.(仅仅连接一个角不算相连)比如,[图2.jpg],[图3.jpg]中,粉红 ...

  6. rocketmq消费负载均衡--push消费为例

    本文介绍了DefaultMQPushConsumerImpl消费者,客户端负载均衡相关知识点.本文从DefaultMQPushConsumerImpl启动过程到实现负载均衡,从源代码一步一步分析,共分 ...

  7. 前Forward / 延时Deferred

    本章节描述了延时光照的渲染路径的细节,如果想了解延迟光照技术,请查阅Deferred Lighting Approaches article. Deferred Lighting is renderi ...

  8. hibernate 反向生实体类 and 为什么老是多一个id

    hibernate 反向生实体类 and 为什么老是多一个id 2017年04月01日 20:32:51 阅读数:548

  9. 安装运行okvis odometry

    源码链接https://github.com/ethz-asl/okvis 1. 安装依赖项 sudo apt-get install cmake sudo apt-get install libgo ...

  10. thinkpad t480s重装win10后小红点失灵 无法启用

    问题描述: thinkpad t480s重装win10纯净版系统后,小红点失效,控制面板中Track Point设置页面被禁用. 解决方法: 可打开下述网址,下载并安装TrackPoint Firmw ...