简体   繁体   中英

Implementation of built-in python modules

I have just started learning python. I want to understand how some functions work and how are modules organized. How can I read the implementations of built-in modules?

Where the source code of the Python standard library is located will depend on your operating system and on how you installed Python. However, the following locations are common:

  • Windows - C:\\Python27\\Lib
  • Linux - /usr/lib/python2.7/

Note that some builtins such as the math module are missing -- that's because those builtins are written in C and are baked directly into the interpreter for purposes of speed.

You should also consider taking a look at the source code for some popular 3rd party libraries. They'll vary in quality, but might be worth examining. Here's a list to help you get started.

There are many implementations of Python, such as CPython, IronPython, PyPy, Jython. The most commonly used Python is CPython. Its source code can by found at hg.python.org .

Your installation also contains source code. For example, to find the source code associate with the collections module, type the following in an interactive session:

>>> import collections
>>> collections
<module 'collections' from '/usr/lib/python2.7/collections.pyc'>

Thus you would look in '/usr/lib/python2.7/collections.py' for the source code associated with the collections module. (Note that you should remove the c in pyc from the path. The .py file is Python source code, the .pyc is byte code.)

A clean way to read this code is in the Python Mercurial repository, or in the Git mirror. (I personally find the Git mirror easier to use, but both are equally good for learning the code.)

In both of these repositories, the Lib folder is the Python standard library.

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