将用户定义的方法添加到TextIOWrapper

时间:2018-12-23 16:01:22

标签: python-3.x

我想将自己的方法添加到TextIOWrapper类

import io


def text(self):
    return ''.join(self.readlines())


io.TextIOWrapper.text = text

但这会导致错误:

Traceback (most recent call last):
File ".../main.py", line 13, in <module>
io.TextIOWrapper.text = text
TypeError: can't set attributes of built-in/extension type '_io.TextIOWrapper'

也许还有其他选择?我想写这段代码:

file = open('some_file')
text = file.text() # not this: text = text(file)

0 个答案:

没有答案