[英]How to instantiate an io.TextIOWrapper object with a name attribute?
import sys
print(sys.stdin)
print(type(sys.stdin))
print(sys.stdin.name)
print(sys.stdin.__dict__)
When the above is executed, the following is the output:执行上述操作后,输出如下:
<_io.TextIOWrapper name='<stdin>' mode='r' encoding='UTF-8'>
<class '_io.TextIOWrapper'>
<stdin>
{'mode': 'r'}
So from the above snippet and output, I can see that name
is an attribute to the _io.TextIOWrapper
instance representing sys.stdin
.所以从上面的片段和输出中,我可以看到
name
是_io.TextIOWrapper
实例的一个属性,代表sys.stdin
。 And from the documentation on io.TextIOWrapper
(via $ pydoc io.TextIOWrapper
for ex.), it does list name
as a data descriptor.从
io.TextIOWrapper
上的文档(例如通过$ pydoc io.TextIOWrapper
) $ pydoc io.TextIOWrapper
,它确实将name
列为数据描述符。 However for whatever reason, name
doesn't show up as an item in its __dict__
.但是,无论出于何种原因,
name
都不会在其__dict__
显示为项目。
When I create an instance of io.TextIOWrapper
manually using for example:当我手动创建一个
io.TextIOWrapper
实例时,例如:
import io
a = io.TextIOWrapper(io.BytesIO())
print(a)
a.name
<_io.TextIOWrapper encoding='UTF-8'>
is printed. <_io.TextIOWrapper encoding='UTF-8'>
被打印出来。 But the a.name
line throws the error: AttributeError: '_io.BytesIO' object has no attribute 'name'
;但是
a.name
行抛出错误: AttributeError: '_io.BytesIO' object has no attribute 'name'
; the AttributeError
I expected, but I didn't it expect to say it was an _io.BytesIO
object.我预期的
AttributeError
,但我没想到它会说它是一个_io.BytesIO
对象。
I'd then tried creating a subclass and attaching a name
attribute manually, like so:然后我尝试创建一个子类并手动附加
name
属性,如下所示:
import io
class NamedTextIOWrapper(io.TextIOWrapper):
def __init__(self, buffer, name=None, **kwargs):
self.name = name
io.TextIOWrapper.__init__(self, buffer, **kwargs)
input = io.BytesIO('abc')
stdin = NamedTextIOWrapper(input, name='<stdin>', encoding='utf-8')
print(stdin.name)
However this runs into: AttributeError: attribute 'name' of '_io.TextIOWrapper' objects is not writable
.但是,这会遇到:
AttributeError: attribute 'name' of '_io.TextIOWrapper' objects is not writable
。
Ideally, I'd also like to be able to maintain the mode
attribute seemingly available in the sys.stdin
instance in a manually instantiated io.TextIOWrapper
object as well.理想情况下,我还希望能够在手动实例化的
io.TextIOWrapper
对象中维护sys.stdin
实例中看似可用的mode
属性。 And also for the sys.stdout
equivalent, which I assume would be just the same except the name
should just be set to '<stdout>'
and the mode
to 'w'
.而且对于
sys.stdout
等效项,我认为除了name
应设置为'<stdout>'
并将mode
为'w'
外,我认为它们是相同'w'
。
You can override the __getattribute__
method with one that returns the name
key of the object's attribute dictionary when the name
attribute is requested:您可以使用一个在请求
name
属性时返回对象属性字典的name
键的方法来覆盖__getattribute__
方法:
class NamedTextIOWrapper(io.TextIOWrapper):
def __init__(self, buffer, name=None, **kwargs):
vars(self)['name'] = name
super().__init__(buffer, **kwargs)
def __getattribute__(self, name):
if name == 'name':
return vars(self)['name']
return super().__getattribute__(name)
so that:以便:
input = io.BytesIO(b'abc')
stdin = NamedTextIOWrapper(input, name='<stdin>', encoding='utf-8')
print(stdin.name)
outputs:输出:
<stdin>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.