13.11继承
包装实际就是对标准类型,增加新的属性,删除不要的属性或修改已存在的属性。
接下来就是书中的三个例子
#-*-coding: utf-8-*- class WrapMe(object): def __init__(self, obj): self.__data = obj def get(self): # 访问实际对象 return self.__data def __repr__(self): return 'self.__data' def __str__(self): return str(self.__data) def __getattr__(self, attr): # 重制__getattr__,将输出的属性设置为self.__data这个对象的属性 return getattr(self.__data, attr) if __name__ == "__main__": wrappedComplex = WrapMe(3.5+4.2j) print wrappedComplex # 包装对象——复数3.5+4.2j print wrappedComplex.real # 实部属性,在类WrapMe中根本没给出明确定义 print wrappedComplex.imag # 虚部属性,同上 print wrappedComplex.conjugate() # 共轭复数方法,同上 print wrappedComplex.get() # 实际对象 wrappedList = WrapMe([123, 'foo', 45.67]) wrappedList.append('bar') # append()方法,在类WrapMe中根本没有给出明确定义 wrappedList.append(123) print wrappedList wrappedList.index(45.67) # index()方法,同上 wrappedList.count(123) # count()方法,同上 wrappedList.pop()# pop()方法,同上 print wrappedList # wrappedList[3] # 切片操作要求__getitem__方法
#-*-coding: utf-8-*- from time import time, ctime class TimeWrapMe(object): def __init__(self, obj): self.__data = obj self.__ctime = self.__mtime = self.__atime = time() # 将创建时间,上次修改时间和上次访问时间设置为当前时间 def get(self): self.__atime = time() # 将上次访问时间设置为运行该方法时的时间 return self.__data def gettimeval(self, t_type): # 带字符参数'c'、'm'或'a',返回相应的时间 if not isinstance(t_type, str) or t_type[0] not in 'cma': raise TypeError, "argument of 'c', 'm', or 'a' req'd" return getattr(self, '_%s__%stime' % (self.__class__.__name__, t_type[0])) def gettimestr(self, t_type): # 获取时间字符串 return ctime(self.gettimeval(t_type)) def set(self, obj): self.__data = obj self.__mtime = self.__atime = time() # 将上次修改时间和上次访问时间设置为运行该方法时的时间 def __repr__(self): self.__atime = time() # 将上次访问时间设置为运行该方法时的时间 return 'self.__data' def __str__(self): self.__atime = time() # 将上次访问时间设置为运行该方法时的时间 return str(self.__data) def __gatattr__(self, attr): self.__atime = time() # 将上次访问时间设置为运行该方法时的时间 return getattr(self.__data, attr) if __name__ == "__main__": timeWrappedObj = TimeWrapMe(932) print timeWrappedObj.gettimestr('c') print timeWrappedObj.gettimestr('m') print timeWrappedObj.gettimestr('a') print timeWrappedObj print timeWrappedObj.gettimestr('c') print timeWrappedObj.gettimestr('m') print timeWrappedObj.gettimestr('a') timeWrappedObj.set('time is up!') print timeWrappedObj.gettimestr('m') print timeWrappedObj print timeWrappedObj.gettimestr('c') print timeWrappedObj.gettimestr('m') print timeWrappedObj.gettimestr('a') #-*-coding: utf-8-*- class CapOpen(object): def __init__(self, fn, mode = 'r', buf = -1): self.file = open(fn, mode, buf) def __str__(self): return str(self.file) def __repr__(self): return 'self.file' def write(self, line): # 将大写的line写入文件 self.file.write(line.upper()) def __getattr__(self, attr): return getattr(self.file, attr) if __name__ == "__main__": f = CapOpen('example13-8.txt', 'w') f.write('delegation example\n') f.write('faye is good\n') f.write('at delegating\n') f.close() f = open('example13-8.txt', 'r') # 如果使用CapOpen会报错,显示这个实例是不可迭代的,是类CapOpen中没有定义__iter__()方法? for eachLine in f: print eachLine, f.close()