1. Python视频
B. 装饰器
装饰器实例:用户管理程序
LOGIN_USER = {'is_login': False}
def outer(func):
def inner(*args, **kwargs):
if LOGIN_USER['is_login']:
r = func()
return r
else:
print('please login')
return inner
@outer
def order():
print('welcome %s'%LOGIN_USER['current_user'])
@outer
def change_pwd():
print('welcome %s'%LOGIN_USER['current_user'])
@outer
def manager():
print('welcome %s'%LOGIN_USER['current_user'])
def login(user, pwd):
if user == 'alex' and pwd == '123':
LOGIN_USER['is_login'] = True
LOGIN_USER['current_user'] = user
print('welcome %s' % LOGIN_USER['current_user'])
def main():
while True:
inp = input('1.login 2.manager 3.change pwd 4.order')
if inp == '1':
username = input('input username')
pwd = input('input password')
login(username, pwd)
elif inp == '2':
manager()
elif inp == '3':
change_pwd()
elif inp == '4':
order()
main()
2. time module
import time
start = time.clock()
...
...
end = time.clock()
print( 'program processing time: %f'%(end - start))