Reputation: 57
I just started learning python not too long ago, and I was hoping to learn more about how the functions and methods of the modules I import actually work.
I was hoping that python libraries would kind of be like javascript frameworks- where i can go into an imported file and see the actual code that the framework/module is built with.
But when i dug around in the python sub directories, i found that the modules were in .lib format.
So are standard library modules actual python code that i could somehow inspect the functions of? Or are they too deeply integrated for me to be able to study them like that?
On a side note, would the same rules apply for non-standard library imports, i.e. beautifulsoup? I was kind of hoping to look at the code to understand how webscraping works from the ground up
Thanks
Upvotes: 3
Views: 4764
Reputation: 1718
As mentioned in the comment above you can look at additional packages such as beautifulsoup in your site-packages folder.
Standard library modules can be written in pure Python, but not all are. When you want to take a look at such modules you can start and browse the CPython source code online (http://hg.python.org/cpython/file/).
There you find different folders:
Lib/
: Many of the pure Python modules (for example the os
or copy
module)Objects/
: Here are the C source files for things such as lists
or dictionaries
If you want to have a look at a module from the standard library, try if you find a file like os.py
for the os
module in the Lib/
-folder.
If you installed the module (either with pip
or any other package manager) locate your site-packages
folder and look for the source files of the module there.
This should give you a good starting point.
Upvotes: 2