[英]What is exactly a file-like object in Python?
在http://docs.python.org/library/json.html :
simplejson.load(fp[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, use_decimal[, **kw]]]]]]]]])
将 fp(一个支持 .read() 且包含 JSON 文档的类文件对象)反序列化为 Python 对象。
我确实知道read()
和write()
做什么。
但是在阅读了这个描述“read()-supporting file-like object”之后,我发现我不知道什么对象类型支持read()
和write()
。
我在其余的文档中找不到。 任何人都可以详细说明该声明吗?
为什么我问这个问题是为了完成“simplejson.load(urllib.open(...))”。
“urllib.open(...)”的返回值不是一个有效的对象,所以我必须为simplejson定制它。 但是,似乎该字符串不支持 read()。
在 Python 中,文件对象是公开 API 的对象,该 API 具有用于执行通常对文件执行的操作的方法,例如read()
或write()
。
在问题的示例中: simplejson.load(fp, ...)
,作为fp
传递的对象只需要有一个read()
方法,可调用方式与文件上的read()
相同(即接受可选参数size
并返回str
或bytes
对象)。
不过,它不需要是一个真实的文件,只要它有一个read()
方法。
类似文件的对象只是file-object的同义词。 请参阅Python 词汇表。
类文件对象主要是StringIO
对象、连接的套接字以及实际的文件对象。
如果一切顺利, urllib.urlopen()
会返回一个支持必要方法的类文件对象。
IO 文档中的IO 类层次结构部分包含一个表格,其中列出了不同类型的类文件对象的内置方法和存根方法。
基本上,抽象基类有一个层次结构:
IOBase
,承诺很少
RawIOBase
,提供无缓冲的二进制 IOBufferedIOBase
,提供缓冲的二进制 IOTextIOBase
,提供缓冲的字符串 IO 要实现类似文件的对象,您可以继承IOBase
的三个后代之一,但不是IOBase
本身。 请参阅此答案以尝试确定给定的类文件对象中的哪一个。
这些类中的每一个都提供了各种存根方法和 mixin:
班级 | 存根方法 | 混合 |
---|---|---|
IOBase |
fileno 号, seek , truncate |
close , closed , __enter__ , __exit__ , flush , isatty , __iter__ , __next__ , readable , readline , readlines , seekable , tell , writable , writelines |
RawIOBase |
readinto , write |
read readall |
BufferedIOBase |
detach , read , read1 , write |
readinto , readinto1 |
TextIOBase |
detach , read , readline , write |
encoding , errors , newlines |
这些方法的文档可以在上面链接的类文档中找到。
这是 Python 中所有类文件对象的 API(从 3.10.5 开始)。
# All file-like objects inherit the IOBase interface:
# Documented at https://docs.python.org/3/library/io.html#io.IOBase .
close() -> None
closed() -> bool # Implemented as @property `closed`
fileno() -> int
flush() -> None
isatty() -> bool
readable() -> bool
readline(size: int = -1) -> Union[str, bytes]
readlines(hint: Union[int, None] = None) -> list
seek(pos: int, whence: int = io.SEEK_SET) -> int # SEEK_SET is 0
seekable() -> bool
tell() -> int
truncate(pos: int = None) -> int # The parameter is named "size" in class FileIO
writable() -> bool
writelines(lines: list) -> None
__del__() -> None
# Documented at https://docs.python.org/3/library/io.html#class-hierarchy .
__enter__()
__exit__(*args) -> None:
__iter__()
__next__() -> Union[str, bytes]
# Documented in paragraph at https://docs.python.org/3/library/io.html#io.IOBase .
# Note that while the documentation claims that the method signatures
# of `read` and `write` vary, all file-like objects included in the Python
# Standard Library have the following exact method signatures for `read` and `write`:
read(size: int = -1) -> Union[str, bytes]
write(b: Union[str, bytes]) -> int # The parameter is named "s" in TextIOBase
特定的类文件对象可能实现的不止于此,但这是所有类文件对象共有的方法的子集。
simplejson 的调用加载和转储消耗和生成字符串,而不是像文件这样的对象。
此链接在 StringIO 和 simplejson 的上下文中为类文件和字符串对象提供了一个示例。
http://svn.red-bean.com/bob/simplejson/tags/simplejson-1.3/docs/index.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.