How to read source code of python?

Floris Bruynooghe floris.bruynooghe at gmail.com
Thu Jun 10 10:53:04 EDT 2010


On Jun 10, 8:55 am, Thomas Jollans <tho... at 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



More information about the Python-list mailing list