标签:方法 http att color enc getattr 没有 产品 class
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 __getattr__(self, item): return getattr(self.file, item) f1 = FileHandle(‘a.txt‘,‘w+‘) print(f1.__dict__) print(‘==>‘,f1.read) f1.write("爱你一万年\n") f1.seek(0) print(f1.read())
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): t = time.strftime(‘%Y-%m_%d %X‘) self.file.write(‘{} {}‘.format(t,line)) def __getattr__(self, item): return getattr(self.file, item) f1 = FileHandle(‘a.txt‘,‘w+‘) print(f1.__dict__) print(‘==>‘,f1.read) f1.write("我和我的猫很想你\n") f1.write("哈哈哈,骗你的\n") f1.write("我没有猫\n") f1.write("也没有你") f1.seek(0) print(f1.read())
标签:方法 http att color enc getattr 没有 产品 class
原文地址:https://www.cnblogs.com/dabai123/p/11615759.html