本帖最后由 阿奇_o 于 2021-8-20 16:24 编辑
暂时你不需要明白,_io.TextIOWrapper
真要深究单看这文档https://docs.python.org/3/library/io.html#io.TextIOWrapper
也不完全能解答,因为前面还有个 _io.
所以,你只要知道它是个"对象",是个叫"TextIOWrapper"的对象,然后可以干嘛用的,就行。
其次,str(obj) 其实是 会调用对象obj的__str__方法,故结果是根据__str__方法的定制来的(自定义类里你可以随便怎么写),
如
- In [4]: with open('t.txt', 'r', encoding='utf-8') as f:
- ...: print(type(f))
- ...: print(f.__str__())
- ...:
- <class '_io.TextIOWrapper'>
- <_io.TextIOWrapper name='t.txt' mode='r' encoding='utf-8'>
- >>> class Foo:
- def __str__(self):
- return "我是 假的 <_io.TextIOWrapper name='t.txt' mode='r' ..>"
- >>> print(Foo) # 类的
- <class '__main__.Foo'>
- >>> print(str(Foo())) # 对象的 .__str__()
- 我是 假的 <_io.TextIOWrapper name='t.txt' mode='r' ..>
- >>> f = Foo()
- >>> print(f)
- 我是 假的 <_io.TextIOWrapper name='t.txt' mode='r' ..>
- >>>
- >>>
复制代码