What is exactly a file-like object in Python?

后端 未结 4 1689
说谎
说谎 2020-12-31 03:15

In http://docs.python.org/library/json.html:

simplejson.load(fp[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_

4条回答
  •  温柔的废话
    2020-12-31 03:46

    In Python, a file object is an object exposing an API having methods for performing operations typically done on files, such as read() or write().

    In the question's example: simplejson.load(fp, ...), the object passed as fp is only required to have a read() method, callable in the same way as a read() on a file (i.e. accepting an optional parameter size and returning either a str or a bytes object).

    This does not need to be a real file, though, as long as it has a read() method.

    A file-like object is just a synonym for file-object. See Python Glossary.

提交回复
热议问题