9

I'm currently learning Python and I want to gain a deeper understanding of how python works by reading its source code.

I could manually go to the directory where Python is installed and check out the source code

enter image description here

I was wondering, is it possible to read Python source code directly from IDE such as PyCharm?

I tried to control click on a method name, even though it did bring me to the method definition page, it did not contain any implementation code

enter image description here

Edit 1

I understand a large of python (cpython to be exact) is implemented in c. Is there anyway to read the c code in IDE such as PyCharm?

5
  • 2
    A lot of Pythons internal functions are actually implemented in C Commented Dec 20, 2017 at 7:14
  • please correct me if im wrong, i'm guessing that the reason why no code is shown for split method of str class is because split method is implemented in c? Commented Dec 20, 2017 at 7:17
  • 2
    It's possible, yes. The python documentation does link you to the sources, I think, if you want to compare Commented Dec 20, 2017 at 7:20
  • thanks for the tip! I will definitly check it out :) Commented Dec 20, 2017 at 7:29
  • 2
    Python's built-in types are implemented in C, so things like str.split() are not so easily mapped to the source code. What you see instead is the PyCharm internal representation of those objects, there only to help autocompletion and inline help. Commented Dec 23, 2017 at 23:13

3 Answers 3

18
+50

As you said, a large chunk of python is written in C but these are not included with the distribution. So you can not read the source from your IDE. These are mostly compiled sources which means only the bytecode is used by the Interpreter. Not all the functions are written in C, most of it is actually written in pure python.

One way to view source is through terminal by using ipython.

In [10]: import string
In [11]: string.lower??
Signature: string.lower(s)
Source:   
def lower(s):
    """lower(s) -> string

Return a copy of the string s converted to lowercase.

"""
return s.lower()
File:      /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/string.py
Type:      function

In [12]: import os

In [13]: os.system??
Docstring:
system(command) -> exit_status

Execute the command (a string) in a subshell.
Type:      builtin_function_or_method

Alternatively you can find what you want, by using inspect.getsource().

import inspect
import os
print (inspect.getsource(os))
# You should see the source code here

As in the above examples, this doesn't work for some built in functions.

To check what is written in C, you should use the other way which is to check this link which is the official repository for the python interpreter. Most of the object types are listed in the cpython/Objects folder. For instance, this is the implementation of list object in C, and for the builtin modules refer this one.

I believe this is what you want. Do comment if you need help.

Sign up to request clarification or add additional context in comments.

Comments

6

Normally PyCharm does what you want, unless the Ctrl+Click leads you to a C function. In this case you will get only minimal information on the function signature and return value, etc., just like you show in your post. built-ins are mainly written in C for an efficiency reason, so if you want to see the C code you'll have to download CPython source code and dive into it. Another alternative would be to download the PyPy source code and take a look at it. Still, you can use PyCharm to read code from modules written in Python in the standard library and that would be helpful in your learning process. Believe me, you'll find good quality code there.

Comments

1

I think Python is so friendly because of its high degree of encapsulation. It is also precisely because we do not always too much concerned about the underlying implementation, and improve our efficiency. So you jump to the method definition page by clicking the method name and understand that the code for this page is sufficient. Unless you want to participate in the development of the Python language itself.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.