'''''''''类装饰器'''class Test(): def __init__(self,func): print('---初始化---') print('func name is %s'%func.__name__) self.__func = func def __call__(self, *args, **kwargs): print('---类装饰器中的内容----') self.__func@Test #至关于test = Test(test)def test(): print('---test---')test() #调用Test类中的__call__方法。'''Python中万物皆对象,类也是一个对象,以下面一个类在定义后,他就是一个Person对象'''print('--------------------1---------------------')class Person(): print('----xxxxxx-------') def __init__(self): self.name = 'an''''元类:元类就是用来建立类(对象)的,元类就是类的类。'''print('--------------------2---------------------')def printNum(self): print('----num-%d-----'%self.num)Test3 = type('Test3',(),{'printNum':printNum}) #type()就是来定义元类的,第一个参数:类名,第二个参数:父类,第三个参数:属性名或方法名。t3 = Test3()t3.num = 100t3.printNum()#下面代码就和上边元类定义类效果同样,可是元类将类定义和方法定义分开,方便维护。class printNum2(): def printNum(self): print('----num-%d-----' % self.num)t2 = printNum2()t2.num = 100t2.printNum()#元类:metaclass_recvdef upper_attr(a,b,c): #第一个参数:类名,第二个参数:类的父类,第三个参数:类的属性。 #便利属性字典,把不是__开头的属性名字变为大写 newAttr = {} for name,value in c.items(): if not name.startswith("__"): newAttr[name.upper()] = value #调用type来建立一个类 return type (a,b,newAttr)class Foo(metaclass=upper_attr): #设置Foo类的元类为upper_attr,做用是,建立类的时候决定建立的类是什么样子的, #建立类的时候先执行metaclass属性对应的东西。 bar = 'bip'print(hasattr(Foo,'bar'))print(hasattr(Foo,'BAR'))f = Foo()print(f.BAR)'''垃圾回收GC:引用计数为主,隔代回收为辅。'''print('--------------------3---------------------')#引用计数机制:对象引用计数为0时,垃圾回收。但解决不了循环引用。import gcclass ClassA(): def __init__(self): print('object born,id:%s'%str(hex(id(self))))def f2(): while True: c1 = ClassA() c2 = ClassA() c1.t = c2 c2.t = c1 del c1 del c2 gc.collect() #手动进行垃圾回收# gc.disable() 关闭GC gc.enable() 开启GC# f2()import sysa = ClassA()print(sys.getrefcount(a)) #查看对象引用个数#所谓垃圾回收就是调用对象的__del__方法。'''内建属性'''print('--------------------4---------------------')class Itcast(): def __init__(self,subject1): self.subject1 = subject1 self.subject2 = 'cpp' #属性访问时拦截器,打log def __getattribute__(self, item): #item--->"subject2" if item == 'subject1': print('log subject1') return 'redirect python' else: #测试时注释掉这2行,将找不到subject2 return object.__getattribute__(self,item) #调用父类object的方法。 def show(self): print('this is Itcast')s = Itcast("python")print(s.subject1)print(s.subject2)s.show() #先获取show属性对应的结果,,,应该是一个方法;方法()。'''内建方法'''print('--------------------5---------------------')print(range(1,10)) #range()返回的是一个迭代值,何时用何时生成值,同生成器原理。 # 若是想的获得列表,用list(rang())print(list(range(1,8,2))) #第三个参数是步长#map():根据原有数据获得新的数据m = map(lambda x : x*x ,[1,2,3]) #[1,2,3]可迭代,但不是迭代对象for i in m: print(i)m2 = map(lambda x,y : x+y,[1,2,3],[4,5,6])for i in m2: print(i)def f1(x,y): return (x,y)l1 = [0,1,2,3,4,5,6]l2 = ['Sun','M','T','W','T','F','S']l3 = map(f1,l1,l2)print(list(l3))#fileter():有筛选功能l4 = filter(lambda x : x%2,[1,2,3,4]) #function接受一个参数,返回布尔值True或False。print(list(l4))l5 = filter(None,"she") #参数为None时不过滤print(list(l5))from functools import reduceprint(reduce(lambda x,y : x+y,[1,2,3,4]))print(reduce(lambda x,y : x+y,['aa','bb','cc'],'dd'))ss = [1,3,2,5,4]print(sorted(ss)) #返回一个排序以后的新的列表print(ss)ss.sort() #将列表排序print(ss)'''集合'''print('---------------------6--------------------')aaa = [11,22,11,22,33]print(set(aaa)) #集合去重#集合运算&、|、aa = {'a','b','c'}bb = {'a','c','d'}print(aa&bb)print(aa|bb)print(aa-bb)print(aa^bb) #对称差集'''functools模块'''print('---------------------6--------------------')import functoolsprint(dir(functools)) #functools模块中经常使用函数#偏函数def showarg(*args,**kwargs): print(args) print(kwargs)p1 = functools.partial(showarg,1,2,3) #只须要传一次参数,后边再去调用这个函数的时候,相应的参数就不用再传了。p1()p1(4,5,6)p1(a='python',b='itcast')#wraps函数def note(func): "note function" # @functools.wraps(func) #当加上这行,查看test()的说明文档时就会看到它本来的注释。 def wrapper(): "wrapper function2222" print('note something') return func() return wrapper@notedef test(): "test function" print('I am test')#使用装饰器时,有一些细节须要被注意,例如,被装饰后的函数其实已是另一个函数了(函数名等函数属性会发生改变)。#添加后因为函数名和函数的doc发生了改变,对测试结果有一些影响。print(help(test)) #此处函数的说明文档是装饰器中的说明文档,而非test()的本来的说明文档注释。'''常见模块'''print('---------------------7--------------------')#经常使用标准库:#builtins:内建函数默认加载 os:操做系统接口 sys:Python自身的运行环境#functools:经常使用的工具 json:编码和解码JSON对象 logging:记录日志、调试#multiprocessing:多进程 threading:多线程 copy:拷贝#time:时间 datetime:日期和时间 calendar:日历#hashlib:加密算法 random:生成随机数 re:字符串正则匹配#socket:标准的BSD Sockets API shutil:文件和目录管理 glob:基于文件通配符搜索import hashlibm = hashlib.md5() #建立hash对象:md5:(message-Digest Algorithm 5)消息摘要算法,得出一个128位的密文。print(m) #<md5 HASH object>m.update(b'itcast') #更新哈希对象以字符串参数print(m.hexdigest())#经常使用第三方扩展库:#requests:使用的是urllib3,继承了urllib2的全部特性 urlib:基于http的高层库 scrapy:爬虫#beautifulsoup4:HTML/XML的解析器 celery:分布式任务调度模块 redis:缓存#Pillow(PIL):图像处理 xlsxwriter:仅写excel功能,支持xlsx xlwt:仅写Excel,支持xls,2013或更早版office#xlrd:仅读Excel功能 elasticsearch:全文搜索引擎 pymysql:数据库链接库#mongoengine/pymongo:mongodbpython接口 matplotlib:画图 numpy/scipy:科学计算#diango/tornado/flask:web框架 xmltodict:xml转dict SimpleHTTPServer:简单的HTTPServer,不使用Web框架#gevent:基于协程的python网络库 fabric:系统管理 pandas:数据处理库#scikit-learn:机器学习库