简体   繁体   中英

I don't understand where the get method of the requests object supported by the requests package for Python gets its code

I have just installed the requests package. It works, meaning that I can run the first two instructions from the documentation without error, namely import requests and requests.get.

I don't understand the underlying source. I see a directory in site-packages named requests. It contains the necessary __init__.py so the system should accept it as a valid object for an import. However, I would expect to see a get.py in the directory to correspond to the get in the script. But no such thing exists. In fact the whole directory is amazingly small. Where is everything?

Take a look at requests.__init__.py but specifically line 123:

from .api import request, get, head, post, patch, put, delete, options

Then if you go into requests.api.py you'll find get() , which is the source implementation for that method:

def get(url, params=None, **kwargs):
    r"""Sends a GET request.
    :param url: URL for the new :class:`Request` object.
    :param params: (optional) Dictionary, list of tuples or bytes to send
        in the query string for the :class:`Request`.
    :param \*\*kwargs: Optional arguments that ``request`` takes.
    :return: :class:`Response <Response>` object
    :rtype: requests.Response
    """

    kwargs.setdefault('allow_redirects', True)
    return request('get', url, params=params, **kwargs)

I'm pretty sure by importing get() into __init__.py that makes it available/invokable via the requests module you initially imported.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM