标签:
day5 主要内容:
1作业重点
2装饰器
单层装饰器
多层装饰器
3模块
内置模块
自定义模块
第三方模块
4字符串格式化
http://www.cnblogs.com/wupeiqi/articles/5484747.html
1顺序传参数
2指定名称传参
3保留小数点后几位
4如果出现占位符,则写%% 打印出一个%
5生成器和迭代器
6其他
递归
冒泡排序
正文开始:
判断登录
判断权限
双层装饰器
解释是从下往上
执行是从上往下的
USER_INFO = {} USER_INFO[‘is_login‘] = True USER_INFO[‘user_type‘] = 2 def check_login(func): def inner(*args,**kwargs): if USER_INFO.get(‘is_login‘,None): ret = func(*args,**kwargs) return ret else: print(‘请登录‘) return inner def check_admin(func): def inner(*args,**kwargs): if USER_INFO.get(‘user_type‘,None) == 2: ret = func(*args,**kwargs) return ret else: print(‘权限不够‘) return inner @check_login @check_admin def index(): print(‘index‘) index()
字符串格式化
%
保留小数位
%c Asc码 %o 8进制 %x 16进制
当格式化时,字符串中出现占位符%是 需要打印2个%%来转义成一个%
%%转义
tp1 = ‘asdsadsa %s 123123‘ % ‘alex‘ print(tp1) tp2 = ‘asdsadsa %s 123123 %d‘ % (‘alex‘,14) print(tp2) tp2 = ‘asdsadsa %.2f 123123‘ % 1.66666 print(tp2) tp2 = ‘asdsadsa %(pp).2f 123123‘ % {‘pp‘ : 1.33232} print(tp2) tp2 = ‘asdsadsa %s %%123123‘ % ‘alex‘ print(tp2)
常用的格式化:
Format:
Format支持转换为2进制。
常用:
tpl = "-----{:*^20s}======={:+d}======={:x}".format(‘alex‘,123,15) print(tpl) tpl = ‘12321321321 {:.2%}‘.format(0.123123) print(tpl) tpl = ‘I am {},age {},{}‘.format(‘seven‘,15,‘alex‘) print(tpl) tpl = ‘I am {},age {},{}‘.format(*[‘seven‘,15,‘alex‘]) print(tpl) tpl = ‘i am {0},age {1},{0}‘.format(‘seven‘,15) print(tpl) tpl = ‘i am {name},age {age},{name}‘.format(**{‘name‘:‘alex‘,‘age‘:17}) print(tpl) tpl = ‘i am {name},age {age},{name}‘.format(name = ‘alex‘,age = 17) print(tpl) tpl = ‘i am {0[0]},age{0[1]}‘.format([1,2,3],[1,3,4,5,56]) print(tpl) tpl = ‘i am {:s},age is {:d} . money {:.2f}‘.format(‘seven‘,15,123123.234) print(tpl) tpl = "{:b},{:o},{:d},{:x},{:X},{:%}".format(15,16,15,18,5,15.33) print(tpl) tpl = ‘{0:b},{0:o},{0:b},{0:x},{0:x},{0:%}‘.format(15) print(tpl)
生成器:使用函数创造的。
def myrange(arg): start = 0 while True: if start > arg: return yield start start += 1 ret = myrange(3) r = ret.__next__() print(r) r = ret.__next__() print(r) r = ret.__next__() print(r) r = ret.__next__() print(r)
标签:
原文地址:http://www.cnblogs.com/aaron-shen/p/5560544.html