我试图在 python3.6 的交互式 shell 中列出一个对象的所有属性/值:
e = FileExistsError()
dir(e)
给出一个属性列表,如 ['\__cause__',...,'characters_written',...]
但是
e.characters_written
抛出错误:AttributeError: characters_written
什么时候
FileExistsError.characters_written
正常执行。
为什么 characters_written 属性在这种情况下很特殊?
请您参考如下方法:
在文档中写道,characters_written 属性可由 IO 类访问。所以你不能通过 FileExistsError 对象访问它
characters_written An integer containing the number of characters written to the stream before it blocked. This attribute is available when using the buffered I/O classes from the io module (Document Link Here)
这个话题也在这里讨论 https://bugs.python.org/issue30554
