# l = [1,2,3]
# l.__iter_() #iter(l)
# Decorator:it's a function and can add additional functions for other functions
# Two principles:
#. 1): can't modify the source code of the functions which been decotated.
# 2):can't modify the calling mode if the functions which been decorated
# calculate the functing time:
import time def cal(l): start_time = time.time() ret = 0 for i in l: ret += i stop_time = time.time() print('functioning time:%s' %(stop_time-start_time)) return ret print(cal(range(100)))
# The basic knowledge of decorator
# decorator = high stage functions + functions nest + close the package
import time def timmer(func): def wrapper(*args,**kwargs): start_time = time.time() res = func(*args,**kwargs) stop_time = time.time() print('functioning time:%s' %(stop_time-start_time)) return res return wrapper @timetimer # cal = timmer(cal(l)) def cal(l): res = 0 for i in l: time.sleep(0.1) res += i return res res = cal(range(20)) print(res)
# high stage functions:the argument is function or the return value is function
import time def foo(): print('from foo') def timmer(func): start_time = time.time() func() stop_time = time.time() print('the functioning time is %s' %(stop_time-start_time)) # timmer(foo) #wrong:changed the name of asking foo = timmer(foo) foo()
# just using the high stage function can't satisfy the decorator
# function nesting: def father(name): # print('from my father %s' %name) def son(): # name = 'laochen' print('my father is %s' %name) def grandson(): print('my grandfather is %s' %name) grandson() son() father('chen')
# close bag:package the parameters including function names and other arguments;find the parameter in current circle first,if did't find then find in the upper stages
# Add a return value:
import time def timer(func): # timer(test) def wrapper(*args,**kwargs): start_time = time.time() res = func(*args,**kwargs) # take the return value of test() stop_time = time.time() print('the functioning time is %s' %(stop_time-start_time)) return res return wrapper @timer # :test = timer(test) def test(name,age,gender): time.sleep(3) print('the test finished,the name is %s age is %s gender is %s' %(name,age,gender)) return 'this is the return value' res = test('alex',19,'male') print(res)
# reverse the value:
f1 = 1 f2 = 2 f1,f2 = f2,f1 print(f1) print(f2)
# Write a simple background:
user_list =[ {'name':'zxver','passwd':'123'} {'name':'alex','passwd':'123'} {'name':'chen','passwd':'123'} ] # make a name list current_dic = {'username':None,'login':False} # not so many times confirming def auth_func(func): def wrapper(*args,**kwargs): if current_dic['username'] and user_dic['login']: res = func(*args,**kwargs) return res username = iuput('username:').strip() passwd = input('password:').strip() for user_dic in user_list: if username == user_dic['username'] and passwd == user_dic['passwd']: current_dic['username'] = username current_dic['login'] = True res = func(*args,**kwargs) return res else: print('wrong username or password') return wrapper @auth_func def index(): print('Welcome to Jingdong') @auth_func def home(name): print('%s Welcome home' %name) @auth_func def shopping_car(name): print('There are (%s,%s,%s) in %s car' %('cosmetics','fruits','bags',name)) index() home('zxver') shopping_car('zxver')