我想将自己的方法添加到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)