标签:obj line 继承 return 自己的 typeerror ret mod pre
#继承
"""
class LIST(list):
def append(self, p_object):
if not isinstance(p_object,int):
raise TypeError(‘must be int‘)
super().append(p_object)
def insert(self, index, p_object):
if not isinstance(p_object,int):
raise TypeError(‘must be int‘)
super().insert(index,p_object)
li =LIST([1,2,3])
print(li)
#li.append(‘2‘)
li.append(4)
print(li)
li.insert(0,-10)
print(li)
#li.insert(0,‘-1‘)
print(li)
"""
#授权
import time
class Open:
def __init__(self,filepath,m=‘w+‘,encode=‘utf-8‘):
self.filepath =filepath
self.mode =m
self.encoding =encode
self.x =open(filepath,mode =m,encoding=encode)
def write(self,line):
t =time.strftime(‘%Y-%m-%d %X‘)
self.x.write(‘%s %s‘%(t,line))
def __getattr__(self, item):
print(‘%s %s‘%(self,item))#getattr(self.x,item),中的item是字符串
return getattr(self.x,item)#把它转化成self.x.item(属性),
#print(‘%s %s‘(self))
f =Open(‘a‘)
f.write(‘12112121\n‘)
f.write(‘121212121\n‘)
f.seek(0)
print(f.read)#查询f.read,但是没有找到,f.read,所以就触发__getattr__方法
标签:obj line 继承 return 自己的 typeerror ret mod pre
原文地址:http://www.cnblogs.com/IQ-Python/p/6758450.html