1.动态导入模块
# module_t=__import__('m1.t') # print(module_t) # module_t = __import__('m1.t') # print(module_t) # module_t.t.test1() # from m1.t import * # from m1.t import test1,_test2 # # test1() # _test2() # module_t.t.test1() # from m1.t import * # from m1.t import test1,_test2 # test1() # _test2() # import importlib # m=importlib.import_module('m1.t') # print(m) # m.test1() # m._test2() import importlib m = importlib.import_module('m1.t') print(m) m.test1() m._test2()
2.包装标准类型
# class List(list): # def append(self, p_object): # if type(p_object) is str: # # self.append(p_object) # super().append(p_object) # else: # print('只能添加字符串类型') # # def show_midlle(self): # mid_index=int(len(self)/2) # return self[mid_index] class List(list): def append(self, p_object): if type(p_object) is str: super().append(p_object) else: print('只能添加字符串类型') def show_midlle(self): mid_index = int(len(self)/2) return self[mid_index] l1 = List('helloworld') print(l1,type(l1)) print(l1.show_midlle()) # l1.append(111111111111111) l1.append('SB') print(l1) # l2=list('hell oworld') # print(l2,type(l2)) # l1=List('helloworld') # print(l1,type(l1)) # print(l1.show_midlle()) # l1.append(1111111111111111111111) # l1.append('SB') # print(l1)
3.双下划线开头的attr方法:
# class Foo: # x=1 # def __init__(self,y): # self.y=y # # def __getattr__(self, item): # print('执行__getattr__') # # f1=Foo(10) # print(f1.y) # print(getattr(f1,'y')) #len(str)--->str.__len__() # f1.sssssssssssssssssssssssssssssssssssss # class Foo: # x=1 # def __init__(self,y): # self.y=y # # def __delattr__(self, item): # print('删除操作__delattr__') # # f1=Foo(10) # del f1.y # del f1.x # # class Foo: # x=1 # def __init__(self,y): # self.y=y # # def __setattr__(self, key, value): # print('__setattr__执行') # # self.key=value # self.__dict__[key]=value # f1=Foo(10) # print(f1.__dict__) # f1.z=2 # print(f1.__dict__) # class Foo: # def __getattr__(self, item): # print('------------->') # # # print(Foo.__dict__) # print(dir(Foo)) # f1=Foo() # # print(f1.x) #只有在属性不存在时,会自动触发__getattr__ # # del f1.x #删除属性时会触发_delattr__ # # f1.y=10 # f1.x=3 # 设置属性的时候会触发——setattr——— # class Foo: # def __init__(self,name): # self.name=name # def __getattr__(self, item): # print('你找的属性【%s】不存在' %item) # def __setattr__(self, k,v): # print('执行setattr',k,v) # if type(v) is str: # print('开始设置') # # self.k=v #触发__setattr__ # self.__dict__[k]=v.upper() # else: # print('必须是字符串类型') # def __delattr__(self, item): # print('不允许删除属性【%s】' %item) # print('执行delattr',item) # del self.item # self.__dict__.pop(item) # f1=Foo('alex') # f1.age=18 #触发__setattr__ # print(f1.__dict__) # print(f1.name) # print(f1.age) # print(f1.gender) # print(f1.slary) # print(f1.__dict__) # del f1.name # print(f1.__dict__)
4.反射
# class BlackMedium: # feture='Ugly' # def __init__(self,name,addr): # self.name=name # self.addr=addr # # def sell_hourse(self): # print('【%s】 正在卖房子,傻逼才买呢' %self.name) # # def rent_hourse(self): # print('【%s】 正在租房子,傻逼才租呢' % self.name) # # # print(hasattr(BlackMedium,'feture')) # getattr() class BlackMedium: feture='Ugly' def __init__(self,name,addr): self.name = name self.addr = addr def sell_hourse(self): print('[%s]正在卖房子,傻逼才买呢'%self.name) def rent_hourse(self): print('[%s]正在租房子,傻逼才租呢'%self.name) b1 = BlackMedium('万成置地', '天露园') # # b1=BlackMedium('万成置地','天露园') # b1.name--->b1.__dic__['name'] # print(b1.__dict__) # # # b1.name # # b1.sell_hourse # print(hasattr(b1,'name')) # print(hasattr(b1,'sell_hourse')) # print(hasattr(b1,'selasdfasdfsadfasdfasdfasdfasdl_hourse')) # # # # print(getattr(b1,'name')) # print(getattr(b1,'rent_hourse')) # func=getattr(b1,'rent_hourse') # func() # # print(getattr(b1,'rent_hourseasdfsa')) #没有则报错 # print(getattr(b1,'rent_hourseasdfsa','没有这个属性')) #没有则报错 b1.sb = True setattr(b1,'sb',True) setattr(b1,'sb1',123) setattr(b1,'name','SB') setattr(b1,'func',lambda x:x+1) setattr(b1,'func1',lambda self:self.name+'sb') print(b1.func1(b1)) # # # # b1.sb=True # setattr(b1,'sb',True) # setattr(b1,'sb1',123) # setattr(b1,'name','SB') # setattr(b1,'func',lambda x:x+1) # setattr(b1,'func1',lambda self:self.name+'sb') # print(b1.__dict__) # print(b1.func) # print(b1.func(10)) # print(b1.func1(b1)) # del b1.sb # del b1.sb1 # delattr(b1,'sb') # print(b1.__dict__)
5.多态
#_*_coding:utf-8_*_ # __author__ = 'Linhaifeng' # class H2O: # def __init__(self,name,temperature): # self.name=name # self.temperature=temperature # def turn_ice(self): # if self.temperature < 0: # print('[%s]温度太低结冰了' %self.name) # elif self.temperature > 0 and self.temperature < 100: # print('[%s]液化成水' %self.name) # elif self.temperature > 100: # print('[%s]温度太高变成了水蒸气' %self.name) # def aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(self): # pass __author__ = 'shiqianyu' class H2O: def __init__(self,name,temperature): self.name = name self.temperature = temperature def turn_ice(self): if self.temperature<0: print('[%s]温度太低结冰了'%self.name) elif self.temperature>0 and self.temperature<100: print('[%s]液化成水'%self.name) elif self.temperature>100: print('[%s]温度太高变成了水蒸汽'%self.name) def aaaaaaaaaaaaaaa(self): pass class Water(H2O): pass class Ice(H2O): pass class Steam(H2O): pass # class Water(H2O): # pass # class Ice(H2O): # pass # class Steam(H2O): # pass w1=Water('水',25) i1=Ice('冰',-20) s1=Steam('蒸汽',3000) # w1.turn_ice() # i1.turn_ice() # s1.turn_ice() # def func(obj): # obj.turn_ice() # func(w1) #---->w1.turn_ice() # func(i1) #---->i1.turn_ice() # def func(obj): # obj.turn_ice() # # func(w1) # func(i1) # func(s1) def func(obj): obj.turn_ice() func(w1) func(i1) func(s1)
6.封装示范一
#_*_coding:utf-8_*_ # __author__ = 'Linhaifeng' # # class Room: # def __init__(self,name,owner,width,length,high): # self.name=name # self.owner=owner # self.__width=width # self.__length=length # self.__high=high # # def tell_area(self): #此时我们想求的是面积 # return self.__width * self.__length *self.__high # # def tell_width(self): # return self.__width __author__ = 'shiqianyu' class Room: def __init__(self,name,owner,width,length,high): self.name = name self.owner = owner self.__width = width self.__length = length self.__high = high def tell_area(self): return self.__width*self.__length*self.__high def tell_width(self): return self.__width r1 = Room('卫生','alex',100,100,500) # area = r1.__width*r1.__length print(r1.tell_area()) # r1=Room('卫生间','alex',100,100,10000) # arear=r1.__width * r1.__length # print(r1.tell_area())
7.授权
# import time # class FileHandle: # def __init__(self,filename,mode='r',encoding='utf-8'): # # self.filename=filename # self.file=open(filename,mode,encoding=encoding) # self.mode=mode # self.encoding=encoding # def write(self,line): # print('------------>',line) # t=time.strftime('%Y-%m-%d %X') # self.file.write('%s %s' %(t,line)) # # def __getattr__(self, item): # # print(item,type(item)) # # self.file.read # return getattr(self.file,item) import time class FileHandle: def __init__(self,filename,mode='r',encoding='utf-8'): self.file = open(filename,mode,encoding=encoding) self.mode = mode self.encoding = encoding def write(self,line): print('-------------------->',line) t = time.strftime('%Y-%m-%d %X') self.file.write('%s %s'%(t,line)) def __getattr__(self, item): return getattr(self.file,item) f1 = FileHandle('a.txt','w+') f1.write('111111111111111 ') f1.write('cpu负载过高 ') f1.write('内存剩余不足 ') f1.write('硬盘剩余不足 ') f1.seek(0) print(f1.read()) # f1=FileHandle('a.txt','w+') # print(f1.file) # print(f1.__dict__) # print('==>',f1.read) #触发__getattr__ # print(f1.write) # f1.write('1111111111111111 ') # f1.write('cpu负载过高 ') # f1.write('内存剩余不足 ') # f1.write('硬盘剩余不足 ') # f1.seek(0) # print('--->',f1.read())