How to read source code of python?

L

Leon

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.
 
S

Steven D'Aprano

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.

How would you read any other source code?
 
G

geremy condra

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.

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
 
R

rantingrick

I'm trying to read the source code of python.
I read around, and am kind of lost, so where to start?

Leon, if you are a Python newbie and cannot understand/comprehend
Python's syntax read the tutorial first.
 
T

Terry Reedy

How would you read any other source code?

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.
 
B

Benjamin Kaplan

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.
 
I

Ian Kelly

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.

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
 
T

Thomas Jollans

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
 
N

News123

Leon said:
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.
 
P

pradeepbpin

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.


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

Floris Bruynooghe

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
 
L

Lee

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,477
Latest member
ColumbusMa

Latest Threads

Top