我想开始阅读 Python 源代码。我应该从哪里开始
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1728472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
I want to start reading the Python source code. Where should I start
提问by agiliq
I want to start reading the Python source code.
我想开始阅读 Python 源代码。
My experience,
我的经验,
I know Python and Java very well. I know some other languages at various levels of proficiency, but neither C/C+/ particularly well. I studied C in college, but have never professionally programmed in it.
我非常了解 Python 和 Java。我知道其他一些不同程度的语言,但 C/C+/ 都不是特别好。我在大学学习过 C,但从未在其中进行专业编程。
My Reasons for reading this code.
我阅读此代码的原因。
- Understand how python works under the hood.
- Learn C better.
- 了解 python 在底层是如何工作的。
- 更好地学习C。
1 is more important to me than 2.
1对我来说比2更重要。
how should I go about this?
我该怎么办?
采纳答案by Ned Batchelder
Start by learning about the Python C API. It is a large and rich API, and the Python source naturally uses it all over the place. You won't get very far into the Python source code before you have to understand what is meant by Py_INCREF and so on.
首先了解 Python C API。它是一个庞大而丰富的 API,Python 源代码自然到处都在使用它。在您必须了解 Py_INCREF 等的含义之前,您不会深入了解 Python 源代码。
I gave a presentation at Pycon explaining the API: A Whirlwind Excursion through Python C Extensionsthat you might find helpful. C extensions use the same API as the Python code itself.
我在 Pycon 上做了一个介绍 API:A Whirlwind Excursion through Python C Extensions的演讲,您可能会觉得这很有帮助。C 扩展使用与 Python 代码本身相同的 API。
回答by Edan Maor
First, if you're mostly interested in 1, I'd start with reading the Python source of various modules (and not jump straight to the C). Whenever I found myself reading the source of some modules, I've always learned new things about Python programming.
首先,如果您对 1 最感兴趣,我会先阅读各种模块的 Python 源代码(而不是直接跳到 C)。每当我发现自己在阅读某些模块的源代码时,我总会学到有关 Python 编程的新知识。
Second, if you're trying to learn C better, I'd personally suggest something completely different: program in it. Just statically reading source code is notgoing to make you understand C better (or at least, it's a limited approach; it might make you a little better, but there's only so much that reading source will get you).
其次,如果你想更好地学习 C,我个人会建议一些完全不同的东西:在其中编程。仅仅静态地阅读源代码不会让你更好地理解 C(或者至少,这是一种有限的方法;它可能会让你更好一点,但阅读源代码只会让你得到这么多)。
After programming at least a moderately sized project in C, thenI'd start looking at Python source. That's really the only way to know C better, and I really think that reading the C source of Python without knowing C well won't get you very far.
在用 C 编写至少一个中等规模的项目之后,我将开始查看 Python 源代码。这确实是更好地了解 C 的唯一方法,我真的认为在不了解 C 的情况下阅读 Python 的 C 源代码不会让你走得更远。
An idea for a C project
一个 C 项目的想法
In fact, here's an idea for a C project: write a Python interpreter in C. Obviously it's not going to be even close to complete, and this is a pretty hard project, but if you only focus on some parts of the language, I think its a good idea.
事实上,这里有一个 C 项目的想法:用 C 编写一个 Python 解释器。显然它甚至不会接近完成,这是一个非常困难的项目,但如果你只关注语言的某些部分,我认为这是个好主意。
Not only will it help you learn C, it will help you understand Python a lot better even beforelooking at the source: you'll have to have a deeper understanding of a lotof stuff in Python, you'll understand the design tradeoffs in how Python works, etc.
它不仅会帮助您学习 C,甚至会帮助您在查看源代码之前更好地理解 Python :您必须对Python中的很多东西有更深入的了解,您将了解Python 是如何工作的,等等。
Then, when you do finally read Python's code, not only will you understand why some things work that way, you'll probably learn a lot of really cool C techniques that solve problems you had.
然后,当你最终阅读 Python 的代码时,你不仅会理解为什么有些事情会这样工作,你可能会学到很多非常酷的 C 技术来解决你遇到的问题。
回答by Lothar
1) First make sure you can build your own Python and run it into a debugger. So you can not only add print expressions but also break at points and follow the code flow. If you have toolsl that let you trace function calls, perfect, you will need it.
1) 首先确保您可以构建自己的 Python 并将其运行到调试器中。因此,您不仅可以添加打印表达式,还可以在点处中断并遵循代码流。如果您有工具可以让您跟踪函数调用,那么完美,您将需要它。
2) Start with the file that implement the data types. They are very easy to understand and you improve your C language language skills while reading the code.
2) 从实现数据类型的文件开始。它们非常易于理解,您可以在阅读代码的同时提高您的 C 语言技能。
3) Make UML diagrams - simple drawing helper tools like Argo UML or MS Visio can help you here. Write down the code flow.
3) 制作 UML 图 - 简单的绘图助手工具,如 Argo UML 或 MS Visio 可以帮助您。写下代码流程。
4) Read the startup code for python. See what and how the basic infrastructure is initialized.
4)阅读python的启动代码。查看基本基础设施的初始化内容和方式。
6) Ty to understand the Python side 100% - even the harder implementation details, what an AST is and what bound and unbound methods are and how you would implement them. When you have a model in mind how you would write an python interpreter then you can go to the final master step.
6) Ty 100% 理解 Python 方面 - 即使是更难的实现细节,AST 是什么,绑定和未绑定方法是什么以及如何实现它们。当您想到如何编写 Python 解释器的模型时,您就可以进入最后的主步骤。
7) Write a debugger extension with the provided fast debugger C API. This helps you to improve your C skills.
7) 使用提供的快速调试器 C API 编写调试器扩展。这有助于您提高 C 技能。
8) Take the final master step and dive into the heart of the interpreter code. This is even hard to read and understand for a well skilled C programmer. Read how expressions are evaluation and method looksup are cached, frames are setup for scoping rules etc. It's difficult and complex - in terms of complexity and lines of code.
8) 迈出最后一步,深入了解解释器代码的核心。对于熟练的 C 程序员来说,这甚至难以阅读和理解。阅读表达式是如何计算和方法查找是如何缓存的,框架是如何设置范围规则的等等。这很困难也很复杂 - 就复杂性和代码行而言。
9) Start Adobe Photoshop and create a nice looking "Master of Python" diploma and put it on your office wall.
9) 启动 Adobe Photoshop 并创建一个漂亮的“Python 硕士”文凭并将其放在您办公室的墙上。
回答by ardsrk
Download the sourcefrom the Python website. Say you unzipped the source into a directory named Python-3.1.1. I suggest you two starting points within Python source code that would help you explore how Python works under the hood:
从 Python 网站下载源代码。假设您将源文件解压缩到名为 Python-3.1.1 的目录中。我建议您从 Python 源代码中的两个起点开始,以帮助您探索 Python 在底层的工作原理:
Examine how the Python Virtual Machine executes the bytecode generated from the interperter. The Python VM is in the file named Python-3.1.1/Python/ceval.c. The core of the VM is an eval loop that starts at the function PyEval_EvalFrameEx in ceval.c. Read through the source and the inline comments. I am sure you would enjoy it.
Another option is to look at how built-in python data types like lists, dictionaries and sets are implemented. For instance sets are implemented in Python-3.1.1/Objects/setobject.c. The Objects directory contains implementations of other data types as well.
检查 Python 虚拟机如何执行从 interperter 生成的字节码。Python VM 位于名为 Python-3.1.1/Python/ceval.c 的文件中。VM 的核心是一个 eval 循环,它从 ceval.c 中的函数 PyEval_EvalFrameEx 开始。通读源代码和内嵌注释。我相信你会喜欢的。
另一种选择是查看内置的 Python 数据类型(如列表、字典和集合)是如何实现的。例如,集合在 Python-3.1.1/Objects/setobject.c 中实现。Objects 目录也包含其他数据类型的实现。
回答by Mizipzor
The question is quite broad so I guess the best answer is to just download the python sourceand go nuts. Pick a module or section of python you know well and check whats under the hood.
这个问题很广泛,所以我想最好的答案是下载python 源代码然后发疯。选择一个你熟悉的 Python 模块或部分,并检查引擎盖下的内容。