本节重点:

  • 掌握装饰器相关知识

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. Spring 学习记录7 初识XmlWebApplicationContext

    主题 之前Spring相关的一些类,比如Enviromnent,BenFactory都接触了一些.有一些收获.但是直接看代码很多方法都不知为什么这样写.哪里会用到.因为太底层了.不好理解..现在从高层 ...

  2. python中heapq堆的讲解

    堆的定义: 堆是一种特殊的数据结构,它的通常的表示是它的根结点的值最大或者是最小. python中heapq的使用 列出一些常见的用法: heap = []#建立一个常见的堆 heappush(hea ...

  3. 解决linux下root运行Elasticsearch异常

    如果以root身份运行将会出现以下问题 root@yxjay:/opt/elasticsearch-2.3.5/bin# ./elasticsearchException in thread &quo ...

  4. MongoDB系列[2]:MongoDB导入导出以及数据库备份

    PS: 以下所有操作都是基于MongoDB自带的工具进行的,所以操作时一定要手动切换到Mongodb的bin目录下面,并且使用管理员权限运行命令 导出工具 mongoexport 概念: mongoD ...

  5. 前端中this的用法

    this指的是方法下的所有参数 handleDelete(record){ this.XXX.AAA (这个this.XXX指的是handleDelete这个方法的所有参数) (let self = ...

  6. centos6.5 源码安装 mysql

    1.下载源码包 我的版本:mysql-5.6.4-m7.tar.gz 2.安装之前先卸载CentOS自带的MySQL [root@localhost ~]# yum remove mysql 3.编译 ...

  7. 各种mac软件地址

    http://www.sdifenzhou.com/  各种软件地址

  8. Listview getItemViewType的使用

    ListView中有两个可以用来让ListView可以在视图中显示多种布局的方法,分别是getItemType和getViewTypeCount 其中 getItemViewType返回的是有参数po ...

  9. jQuery代码在移动端不运行

    今天写了个html网页发现在iOS系统上边不运行,于是真机连上Sarfari查看报错,于是乎 其实这是由于iOS的安全策略决定的,不允许加载非https的连接 报错:was not allowed t ...

  10. linux系统中的命令替换与整数运算$(),$(())

    一.$()与`` 在 bash shell 中,$( ) 与 ` ` (反引号) 都是用来做命令替换(command substitution)用的. 所谓的命令替换与我们第五章学过的变量替换差不多, ...