Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to read source code of python?

3 views
Skip to first unread message

Leon

unread,
Jun 9, 2010, 8:28:28 PM6/9/10
to
Hi, there,
I'm trying to read the source code of python.
I read around, and am kind of lost, so where to start?

Any comments are welcomed, thanks in advance.

Steven D'Aprano

unread,
Jun 9, 2010, 8:35:31 PM6/9/10
to

How would you read any other source code?

--
Steven

geremy condra

unread,
Jun 9, 2010, 8:59:05 PM6/9/10
to Leon, pytho...@python.org

Are you trying to find out more about python-the-language,
or the interpreter, or the stdlib, or trying to make some
specific change, or...?

Geremy Condra

Message has been deleted

Terry Reedy

unread,
Jun 10, 2010, 1:00:05 AM6/10/10
to pytho...@python.org

I would begin at the beginning, whatever that might be. But the Python
source tree is rather opaque in some respects compared to other stuff I
have read. The individual files I have read are much better organized.


Benjamin Kaplan

unread,
Jun 10, 2010, 1:54:01 AM6/10/10
to pytho...@python.org
On Wed, Jun 9, 2010 at 10:25 PM, Qijing Li <qjin...@gmail.com> wrote:
> Thanks for your reply.
> I'm trying to understand python language deeply and  use it efficiently.
> For example: How the operator "in" works on list? the running time is
> be O(n)?  if my list is sorted, what the running time would be?
>
>

Still O(n). Python doesn't know that your list is sorted, so nothing
changes. And the check to make sure it is sorted would be O(n) anyway.

Ian Kelly

unread,
Jun 10, 2010, 3:23:39 AM6/10/10
to pytho...@python.org

However, if the programmer knows that the list is sorted, then the
following would be O(log n):

from bisect import bisect_left

index = bisect_left(the_list, item)
item_in_list = index < len(the_list) and the_list[index] == item

But in general, if you want the "in" operator to be efficient, then
you probably want to use a set or a dict, not a list.

Cheers,
Ian

Thomas Jollans

unread,
Jun 10, 2010, 3:55:02 AM6/10/10
to pytho...@python.org
On 06/10/2010 07:25 AM, Qijing Li wrote:
> Thanks for your reply.
> I'm trying to understand python language deeply and use it efficiently.
> For example: How the operator "in" works on list? the running time is
> be O(n)? if my list is sorted, what the running time would be?

There is excellent documentation of the language and standard library at
http://docs.python.org/ .

Otherwise, just download the Python source code! You know it's free. I
think it's pretty well organised, though I haven't worked with it a lot
yet myself. Just poke around!

Have fun,
Thomas

>
>
>
> On Wed, Jun 9, 2010 at 5:59 PM, geremy condra <deba...@gmail.com
> <mailto:deba...@gmail.com>> wrote:

News123

unread,
Jun 10, 2010, 4:15:25 AM6/10/10
to
Leon wrote:
> Hi, there,
> I'm trying to read the source code of python.dd

> I read around, and am kind of lost, so where to start?
>
> Any comments are welcomed, thanks in advance.


I use my favourite text editor with syntax highlighting.

Next to it I use a web browser with pydoc and google.

If uou're looking for an IDE that will help you a little more navigating
in python code, then you could look at

Eclipse
or
Netbeans
both support python

both are rather heavy weapons though.

pradeepbpin

unread,
Jun 10, 2010, 7:18:54 AM6/10/10
to


In my opinion, pydoc would be a good choice. I am a fan of it.

Floris Bruynooghe

unread,
Jun 10, 2010, 10:53:04 AM6/10/10
to
On Jun 10, 8:55 am, Thomas Jollans <tho...@jollans.com> wrote:
> On 06/10/2010 07:25 AM, Qijing Li wrote:
>
> > Thanks for your reply.
> > I'm trying to understand python language deeply and  use it efficiently.
> > For example: How the operator "in" works on list? the running time is
> > be O(n)?  if my list is sorted, what the running time would be?

Taking this example, you know you want the "in" operator. Which you
somehow need to know is implemented by the "__contains__" protocol
(you can find this in the "expressions" section of the "Language
Reference").

Now you can either know how objects look like in C (follow the
"Extending and Embedding" tutorial, specifically the "Defining New
Types" section) and therefore know you need to look at the sq_contains
slot of the PySequenceMethods sturcture. Or you could just locate the
list object in Objects/listobjects.c (which you can easily find by
looking at the source tree) and search for "contains". Both ways
will lead you pretty quickly to the list_contains() function in
Objects/listobject.c. And now you just need to know the C-API (again
in the docs) to be able to read it (even if you don't that's a pretty
straightforward function to read).

Hope that helps
Floris

Giampaolo Rodolà

unread,
Jun 10, 2010, 1:26:20 PM6/10/10
to Leon, pytho...@python.org
2010/6/10 Leon <qjin...@gmail.com>:
> --
> http://mail.python.org/mailman/listinfo/python-list
>

If you're interested in understanding Python internals you might want
to take a look at this:
http://tech.blog.aknin.name/category/my-projects/pythons-innards/


--- Giampaolo
http://code.google.com/p/pyftpdlib
http://code.google.com/p/psutil

Lee

unread,
Jun 11, 2010, 3:55:53 AM6/11/10
to
On Jun 10, 7:53 am, Floris Bruynooghe <floris.bruynoo...@gmail.com>
wrote:

It does help, thank you.
I think I know where to start, I found somethings I'm interested in in
listobject.c.

Lee

unread,
Jun 11, 2010, 3:56:34 AM6/11/10
to
On Jun 10, 10:26 am, Giampaolo Rodolà <g.rod...@gmail.com> wrote:
> 2010/6/10 Leon <qjing...@gmail.com>:

Great stuff, which works for me very well.

0 new messages