python文件派生


import time


class Foo:
x = 1

def __init__(self, y):
self.y = y

def __getattr__(self, item): # 没有的情况下调用此方法
print(item)

def __setattr__(self, key, value):
self.__dict__[key] = value

def __delattr__(self, item):
pass


# f1 = Foo(1)
# print(f1.y)
#
# print(dir(Foo.__dict__))


# 文件包装 写入的时候加入时间
class FileHandle:
def __init__(self, filename, mode='r', encoding='utf8'):
self.file = open(filename, mode, 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)


f = FileHandle('a.txt', 'w+', 'utf8')
f.write(1)
f.read()
 
原文地址:https://www.cnblogs.com/majianyu/p/10210851.html