I have some questions about Python's dir function
>>>import urllib
>>>dir(urllib)
['__builtins__', '__cached__', '__doc__', '__file__','__loader__','__name__','__package__', '__path__', '__spec__']
And when I do like this
>>>import urllib.request
>>>dir(urllib)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__','__package__', '__path__', '__spec__', 'error', 'parse', 'request', 'response']
Why it comes three more attributes? Why the request attribute doesn't in the dir(urllib) at first?
I' really appriciate your help!
urllib
is a package, whereas in urllib.request
, request
is a module inside the urllib
package.
When you import a package, it does not automatically import modules inside the package, unless that module is imported by the __init__.py
for that package.
But the __init__.py
of urllib
is empty (its empty in my Python 3.4 ) , and hence when simply importing urllib
it does not import the module urllib.request
.
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.