For example in requests
library we have get
method which is defined inside api.py
file
but i can directly call it with requests.get
.
as per my understanding it should be called like this requests.api.get
How python modules handles this.
For example in requests
library we have get
method which is defined inside api.py
file
but i can directly call it with requests.get
.
as per my understanding it should be called like this requests.api.get
How python modules handles this.
When you are doing import requests
the file requests/__init__.py
is imported and when you access its content with dot notation like requests.get
you in fact are accessing requests.__init__.get
. This contains the following line:
from .api import request, get, head, post, patch, put, delete, options
So this __init__
file has get
name in its scope, thats all. More info about __init__.py
file here: What is __init__.py for?.
Your intuition is correct that the name should be requests.api.get
. In fact, if you look at requests.get.__module__
, you will find that it is requests.api
. Similarly, requests.get.__globals__
is a reference to requests.api.__dict__
.
Many libraries will export functions defined elsewhere through the top level namespace. This is usually done with an import like from .api import get
in the __init__.py
file of requests
.