下面是一个使用io.BufferedReader的最小python示例,它的行为非常奇怪
三次通话结果不一致。2工作,最后一个,最重要的一个没有。我看这个太久了,我看不出问题所在。也许我做错了什么我错过了。请看一下。我正在Ubuntu 18.04.3 LTS上使用Python2.7.15+
注意:结果在产生输出的行下面的####注释中
from io import BytesIO, StringIO, BufferedReader, DEFAULT_BUFFER_SIZE
class MDReader(BufferedReader):
def __new__(cls, thingtoread, buffer_size=DEFAULT_BUFFER_SIZE):
iothing = BytesIO(thingtoread) \
if isinstance(thingtoread, str) \
else StringIO(thingtoread) \
if isinstance(thingtoread, unicode) \
else thingtoread
print iothing
return iothing and BufferedReader.__new__(cls, iothing, buffer_size)
text = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
mdr0 = BufferedReader.__new__(MDReader, BytesIO(text), DEFAULT_BUFFER_SIZE)
### <_io.BytesIO object at 0x7f348d06bbf0>
mdr1 = MDReader(BytesIO(text), DEFAULT_BUFFER_SIZE)
### <_io.BytesIO object at 0x7f348d06ba70>
mdr2 = MDReader(text, DEFAULT_BUFFER_SIZE)
### Traceback (most recent call last):
### File "<stdin>", line 1, in <module>
### File "./foo.py", line 19, in <module>
### mdr2 = MDReader(text, DEFAULT_BUFFER_SIZE)
### AttributeError: 'str' object has no attribute 'readable'
如果您将“新”中的退货替换为:
那么它的行为是正确的。除了我忘了处理初始化外,我什么都没做。或者更好的方法是将参数处理代码移到\uuu init\uuuuu并删除新的\uuuuu
很抱歉发了这个。我想我只是没有看到显而易见的东西,因为我在寻找有价值的东西。哦
相关问题 更多 >
编程相关推荐