views:

480

answers:

5

I want to start reading the Python source code.

My experience,

I know Python and Java very well. I know some other languages at various levels of proficiency, but neither C/C+/ particularly well. I studied C in college, but have never professionally programmed in it.

My Reasons for reading this code.

  1. Understand how python works under the hood.
  2. Learn C better.

1 is more important to me than 2.

how should I go about this?

+2  A: 

The question is quite broad so I guess the best answer is to just download the python source and go nuts. Pick a module or section of python you know well and check whats under the hood.

mizipzor
+1 - LOL at "and go nuts".. :)
Thiyagaraj
I added a comment to original question, which will help me "go nuts"..
uswaretech
You compare your situation to someone who wants to get into the django source, you state that one should then start by looking at "urls.py". Why? Because its the most important? I disagree, I think you should start with a module youre very familiar with, as to better draw conclusions about what result (and how) the under-the-hood algorithms produces.
mizipzor
+8  A: 

First, if you're mostly interested in 1, I'd start with reading the Python source of various modules (and not jump straight to the C). Whenever I found myself reading the source of some modules, I've always learned new things about Python programming.

Second, if you're trying to learn C better, I'd personally suggest something completely different: program in it. Just statically reading source code is not going to make you understand C better (or at least, it's a limited approach; it might make you a little better, but there's only so much that reading source will get you).

After programming at least a moderately sized project in C, then I'd start looking at Python source. That's really the only way to know C better, and I really think that reading the C source of Python without knowing C well won't get you very far.

An idea for a C project

In fact, here's an idea for a C project: write a Python interpreter in C. Obviously it's not going to be even close to complete, and this is a pretty hard project, but if you only focus on some parts of the language, I think its a good idea.

Not only will it help you learn C, it will help you understand Python a lot better even before looking at the source: you'll have to have a deeper understanding of a lot of stuff in Python, you'll understand the design tradeoffs in how Python works, etc.

Then, when you do finally read Python's code, not only will you understand why some things work that way, you'll probably learn a lot of really cool C techniques that solve problems you had.

Edan Maor
+3  A: 

1) First make sure you can build your own Python and run it into a debugger. So you can not only add print expressions but also break at points and follow the code flow. If you have toolsl that let you trace function calls, perfect, you will need it.

2) Start with the file that implement the data types. They are very easy to understand and you improve your C language language skills while reading the code.

3) Make UML diagrams - simple drawing helper tools like Argo UML or MS Visio can help you here. Write down the code flow.

4) Read the startup code for python. See what and how the basic infrastructure is initialized.

6) Ty to understand the Python side 100% - even the harder implementation details, what an AST is and what bound and unbound methods are and how you would implement them. When you have a model in mind how you would write an python interpreter then you can go to the final master step.

7) Write a debugger extension with the provided fast debugger C API. This helps you to improve your C skills.

8) Take the final master step and dive into the heart of the interpreter code. This is even hard to read and understand for a well skilled C programmer. Read how expressions are evaluation and method looksup are cached, frames are setup for scoping rules etc. It's difficult and complex - in terms of complexity and lines of code.

9) Start Adobe Photoshop and create a nice looking "Master of Python" diploma and put it on your office wall.

Lothar
+5  A: 

Start by learning about the Python C API. It is a large and rich API, and the Python source naturally uses it all over the place. You won't get very far into the Python source code before you have to understand what is meant by Py_INCREF and so on.

I gave a presentation at Pycon explaining the API: A Whirlwind Excursion through Python C Extensions that you might find helpful. C extensions use the same API as the Python code itself.

Ned Batchelder
+1 for C API: There's so many opportunities to add batteries to Python it's not funny.
Jed Smith
A: 

Download the source from the python website. Say you unzipped the source into a directory named Python-3.1.1. I suggest you two starting points within Python source code that would help you explore how Python works under the hood:

  • Examine how the Python Virtual Machine executes the bytecode generated from the interperter. The Python VM is in the file named Python-3.1.1/Python/ceval.c. The core of the VM is an eval loop that starts at the function PyEval_EvalFrameEx in ceval.c. Read through the source and the inline comments. I am sure you would enjoy it.

  • Another option is to look at how built-in python data types like lists, dictionaries and sets are implemented. For instance sets are implemented in Python-3.1.1/Objects/setobject.c. The Objects directory contains implementations of other data types as well.

Hope this helps.

Enjoy!!

ardsrk