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