如何直接从 IDE 读取 Python 源代码 [英] How to read Python source code directly from IDE

查看:82
本文介绍了如何直接从 IDE 读取 Python 源代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习 Python,我想通过阅读其源代码来更深入地了解 Python 的工作原理.

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

我可以手动去Python安装目录查看源码

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

我想知道,是否可以直接从 PyCharm 等 IDE 中读取 Python 源代码?

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

编辑 1

我知道大量的python(确切地说是cpython)是用c实现的.有没有办法在 PyCharm 等 IDE 中读取 c 代码?

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?

推荐答案

如您所说,大量 Python 是用 C 编写的,但这些包含在发行版中.所以你不能从你的 IDE 中读取源代码.这些大多是编译源,这意味着解释器只使用字节码.不是所有的函数都是用C写的,大部分其实都是纯python写的.

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.

查看源代码的一种方法是使用 ipython 通过终端.

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

或者,您可以使用 inspect.getsource() 找到您想要的内容.

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.

要检查用 C 编写的内容,您应该使用另一种方式检查此链接这是python解释器的官方存储库.大多数对象类型都列在 cpython/Objects 文件夹中.例如,这是 list object 在 C 中的实现,对于 builtin 模块,请参考这个.

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.

这篇关于如何直接从 IDE 读取 Python 源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆