#包装标准类型 class List(list): def append(self,p_object): if type(p_object) is str: #self.append(p_objec) #无限递归 super().append(p_object) #等价与list.append(p_object) def show_middle(self): mid_index=int(len(self)/2) return self[mid_index] l=List('hello') l.append('SB') print(l) print(l.show_middle()) #组合方式授权 class Filehandle: def __init__(self,filename,mode='r',encoding='utf-8'): self.filename=open(filename,mode,encoding=encoding) self.mode=mode self.encodig=encoding def __getattr__(self,item): return getattr(self.filename,item) f1=Filehandle('aaa.txt') print(f1.read()) #isinstance跟issubclass,type class Foo: pass f1=Foo() print(isinstance(f1,Foo)) class Bar(Foo): pass b1=Bar() print(issubclass(Bar,Foo)) print(type(b1))
Python基础27类-包装、组合方式授权、判断对象类型的方法
猜你喜欢
转载自www.cnblogs.com/josie930813/p/10482035.html
今日推荐
周排行