简体   繁体   English

如何使用 name 属性实例化 io.TextIOWrapper 对象?

[英]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.

相关问题 Python-属性错误&#39;_io.TextIOWrapper&#39;对象没有属性&#39;open&#39; - Python - Attribute Error '_io.TextIOWrapper' object has no attribute 'open' &#39;_io.TextIOWrapper&#39;没有属性&#39;replace&#39; - '_io.TextIOWrapper' no attribute 'replace' Python-错误-AttributeError:“ _ io.TextIOWrapper”对象没有属性“插入” - Python - Error - AttributeError: '_io.TextIOWrapper' object has no attribute 'insert' AttributeError: &#39;_io.TextIOWrapper&#39; 对象没有 txt 文件的属性 &#39;lower&#39; - AttributeError: '_io.TextIOWrapper' object has no attribute 'lower' for txt file _io.TextIOWrapper&#39;对象不可调用 - _io.TextIOWrapper' object is not callable AttributeError: &#39;_io.TextIOWrapper&#39; 对象没有属性 &#39;next&#39; python - AttributeError: '_io.TextIOWrapper' object has no attribute 'next' python AttributeError:&#39;_io.TextIOWrapper&#39;对象没有属性&#39;next&#39;? - AttributeError: '_io.TextIOWrapper' object has no attribute 'next'? Python说:AttributeError:&#39;_io.TextIOWrapper&#39;对象没有属性&#39; - Python says: AttributeError: '_io.TextIOWrapper' object has no attribute ' AttributeError:&#39;_io.TextIOWrapper&#39;对象没有属性&#39;decode&#39; - AttributeError: '_io.TextIOWrapper' object has no attribute 'decode' AttributeError:&#39;_io.TextIOWrapper&#39;对象没有属性&#39;split&#39;错误 - AttributeError: '_io.TextIOWrapper' object has no attribute 'split' error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM