How do modules work in Python? -
i wondering how module work in python.for example have code
import requests r = requests.get("http://www.google.com") print r.status_code
now according understanding, requests module should have python file containing class called "get" , within "get" class there must member variable called "status_code" when create object "r", variable status_code it.
however, when looked @ files come in package, not find class named "get". find function called "get", under class called "response". since did not create object instance of "response" class, how can access "get" function inside it?
i think missing key concept here, can point out me please?
thanks
when import requests
file __init__.py
is executed, if examine file in case, find line:
from .api import request, get, head, post, patch, put, delete, options
which means api.py
importing get()
function:
def get(url, **kwargs): kwargs.setdefault('allow_redirects', true) return request('get', url, **kwargs)
and can see calls request function api.py
looks like:
def request(method, url, **kwargs): session = sessions.session() return session.request(method=method, url=url, **kwargs)
that creates object session
defined inside session.py
, calls method request
. method call method send()
returns response
object defined in class response
inside models.py
(i copy first lines):
class response(object): def __init__(self): super(response, self).__init__() self._content = false self._content_consumed = false #: integer code of responded http status. self.status_code = none ...
here status_code
defined, when invoke r = requests.get("http://www.google.com")
retrieving object , can access status_code
Comments
Post a Comment