In http://docs.python.org/library/json.html:
simplejson.load(fp[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_
simplejson has the calls loads and dumps that consumes and produce strings instead of file like objects.
This link has an example in the context of StringIO and simplejson for both file-like and string objects.
http://svn.red-bean.com/bob/simplejson/tags/simplejson-1.3/docs/index.html
File-like objects are mainly StringIO
objects, connected sockets and, well, actual file objects.
If everything goes well, urllib.urlopen()
returns a file-like object supporting the necessary methods.
From the glossary:
file-like object
A synonym for file object
and a file object is
file object
An object exposing a file-oriented API (with methods such as read() or write()) to an underlying resource. Depending on the way it was created, a file object can mediate access to a real on-disk file or to another type of storage or communication device (for example standard input/output, in-memory buffers, sockets, pipes, etc.). File objects are also called file-like objects or streams.
There are actually three categories of file objects: raw binary files, buffered binary files and text files. Their interfaces are defined in the io module. The canonical way to create a file object is by using the open() function.
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.