python 文件对象的继承
来源:互联网 发布:安全第一网络银行 编辑:程序博客网 时间:2024/06/10 03:03
# inheritance of file type / define subclass of file type
Python 文件对象的方法对很多人来说都不能满足需求。还有文本文件的seek功能有限制,很不方便。于是就应该继承文件对象。一种简单的办法是 wrap。就是让文件对象成为新对象的属性。这样通过 self.fo.method 就可以调用原有的方法同时定义新方法,起到扩展方法的功能。
class myFO: # a wrap of file object def __init__(self,fname,mode='r'): self.fo=open(fname,mode) def blah blah blah
可是我们会觉得这样很啰嗦,read 方法只能通过 self.fo.read 来调用,不能直接 self.read,要么重新定义 read 方法;可是重新定义就没有继承的意义了。因此,本人推荐一种方法,可以直接用 self.read 来调用 self.fo.read, 而不会有危险发生。注意__setattr__会截获任意一次赋值,但是这不会有问题。
class myFO: # a wrap of file object def __init__(self,fname,mode='r'): self.fo=open(fname,mode) def __setattr__(self, attr, val): #print('catch',attr,'when setattr') super().__setattr__(attr,val) # or super(myFO,self) if attr != 'fo': # make sure f.attr == f.fo.attr where attr is not fo setattr(self.fo,attr,val) def __getattr__(self,attr): #print('catch',attr,'when getattr') return getattr(self.fo,attr)
还有就是真正意义上的继承了。但没有想象中那么简单,注意语法!初始化是要用 rb 而不是 r !其实本人也没搞懂buffer,只是一样画葫芦,而且也不知道 io 和 _io 之间的区别。
import io # import _ioclass myFO(io.TextIOWrapper): # (_io.TextIOWrapper) def __init__(self, buffer): super().__init__(buffer) def eof(self): ''' judge fp is at the end of file ''' cont=self.read(1) if cont=='': return True else: pos=self.tell() self.seek(pos-1) return Falsefo=myFO(open('test.txt','rb')) # 注意初始化,用rb # 或者 f=open('test.txt','r');fo=myFO(f.buffer), 请不要关闭f
如果用 fo=myFO(open('test.txt','r')) ,就会有如下报错
Traceback (most recent call last):
File "D:\Python\mytest\cls.py", line 81, in <module>
print(fo.read()) # 报错
TypeError: 'str' does not support the buffer interface
PyPI上能找到 filelike, fileutils。它们都和文件对象有关,提供了很多强大的方法。但是并没有实现真正的继承。还是通过wrap实现。
0 0
- python 文件对象的继承
- 学习Python的对象继承
- python面向对象的继承
- python的类、对象、继承
- python对象的继承和重写
- python对象的继承和重写
- 继承object对象对python多继承的影响
- python简单学习:类、类与对象、继承、读取文件
- python 面向对象 继承
- python 面向对象继承
- Python对象 继承 多态 获取对象信息 类的属性
- Python 类的定义、继承及使用对象
- Python 类的定义、继承及使用对象
- Python 类的定义、继承及使用对象 [转]
- Python一天入门16:面向对象的编程3-继承
- PYthon类的定义、继承以及使用对象(例)
- python 对象的魔力--多态、封装和继承
- python面向对象编程基础II(类的继承)
- Spring MVC讲解
- VS2013中Image Watch插件的使用(OpenCV)
- E - windy数
- C++获取数组大小
- Linux crontab定时执行任务 命令格式与详细例子
- python 文件对象的继承
- 403 Forbidden You don't have permission to access / on this server.
- 手游帧同步的研究
- error C2660: ‘Create’ : function does not take 2 parameters
- 如何体现业务流程的执行力、效率和效益?
- MessageFormat(动态文本)
- i find a nice essay,and i copy it for u
- EF AES Library 的封装类
- 开始使用UIScrollView