#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = '人生入戏'
user = "admin"
passwd = "123456"
def auth(auth_type):
#print("auth_type:",auth_type)
def out_wrapper(func):
#print("func",func)
def wrapper(*args,**kwargs):
#print(args,kwargs)
if auth_type == "1":
username = input("username:").strip()
password = input("password:").strip()
if username == user and password == passwd:
print("start".center(20,"-"))
res = func(*args,**kwargs)#把函数的返回值赋值到res
print("end".center(20,"-"))
return res#返回函数的返回值
else:
print("error!")
elif auth_type =="2":
print('2')
return wrapper
return out_wrapper
def index():
print("index ok")
@auth(auth_type="1")
def home(name):
print("home ok",name)
return 'from home'
@auth(auth_type="2")
def bbs():
print("bbs ok")
index()
print(home("你好"))#打印home函数的返回值
bbs()
'''
装饰器:
定义:本质是函数,就是为了给其他的函数增加其他功能
原则:不能改变被装饰函数的源码和调用方式
'''
'''
一、函数等于'变量'
二、高阶函数:
1.把一个函数名当作实参传给另一个函数(在不修改被装饰函数的代码的情况下增加其他功能)
2.返回值包含函数名(不修改函数的调用方式)
三、嵌套函数
'''
'''
嵌套函数+高阶函数=装饰器
'''