1.動態導入模塊 2.包裝標準類型 3.雙下劃線開頭的attr方法: 4.反射 5.多態 6.封裝示範一 ...
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\n') f1.write('cpu負載過高\n') f1.write('記憶體剩餘不足\n') f1.write('硬碟剩餘不足\n') 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\n') # f1.write('cpu負載過高\n') # f1.write('記憶體剩餘不足\n') # f1.write('硬碟剩餘不足\n') # f1.seek(0) # print('--->',f1.read())