简体   繁体   English

你如何研究python的内置方法的实现?

[英]How do you investigate python's implementation of built-in methods?

I'm currently going through a basic compsci course. 我目前正在修读基础compsci课程。 We use Python's in a lot. 我们使用Python in很多。 I'm curious how it's implemented, what the code that powers in looks like. 我很好奇它是如何实现的,哪些代码权力in模样。

I can think of how my implementation of such a thing would work, but something I've learned after turning in a couple homework assignments is that my ways of doing things are usually pretty terrible and inefficient.. So I want to start investigating 'good' code. 我可以想一想对这样的事情的实现是如何起作用的,但是我在完成一些家庭作业后学到的东西是我做事的方式通常非常糟糕和低效。所以我想开始调查'好'代码。

The thing about builtin functions and types and operators and so on is that they are not implemented in Python. 关于内置函数,类型和运算符等的事情是它们没有用Python实现。 Rather, they're implemented in C, which is a much more painful and verbose programming language that won't always translate well to Python (usually because things are easier some other way in Python.) 相反,它们是用C实现的,这是一种更加痛苦和冗长的编程语言,并不总能很好地转换为Python(通常因为在Python中其他方式更容易)。

With that said, you can investigate all of Python's implementation online, via their public source repository . 话虽如此,您可以通过他们的公共源存储库在线调查Python的所有实现。

The implementation for in is scattered -- there's one implementation per type, plus a more general implementation that calls the type-specific implementation (more on that later). in的实现是分散的 - 每种类型有一个实现,还有一个更通用的实现,它调用特定于类型的实现(稍后会详细介绍)。 For example, for lists, we'd look for the implementation of lists. 例如,对于列表,我们会查找列表的实现。 In the Python source tree, the source for all builtin objects is in the Objects directory. 在Python源代码树中,所有内置对象的源都在Objects目录中。 In that directory you'll find listobject.c , which contains the implementation for the list object and all its methods. 在该目录中,您将找到listobject.c,其中包含列表对象及其所有方法的实现。

On the repository at the time of answering, if you look at line 393 you'll find the implementation of the in operator (also known as the __contains__ method, which explains the name of the function). 在回答时的存储库中,如果你看第393行,你会发现in运算符的实现(也称为__contains__方法,它解释了函数的名称)。 It's fairly straightforward, just loops through all the elements of the list until either the element is found, or there's no more elements, and returns the result of the search. 它相当简单,只是循环遍历列表的所有元素,直到找到元素,或者没有更多元素,并返回搜索结果。 :) :)

If it helps, in Python the idiomatic way to write this would be: 如果它有帮助,在Python中写这个的惯用方法是:

def __contains__(self, obj):
    for item in self:
        if item == obj:
            return True

    return False

I said earlier that there was a more general implementation. 我之前说过,有一个更普遍的实施。 That can be seen in the implementation of PySequence_Contains in abstract.c . 这可以在abstract.cPySequence_Contains的实现中看到。 It tries to call the type-specific version, and if that fails, resorts to regular iteration. 它尝试调用特定于类型的版本,如果失败,则转向常规迭代。 That loop there is what a regular Python for loop looks like when you write it in C (using the Python C-API). 当你用C语言(使用Python C-API)编写时,循环就是常规的Python for循环。

From the Data Model section of the Python Language Reference: 从Python语言参考的数据模型部分:

The membership test operators ( in and not in ) are normally implemented as an iteration through a sequence. 成员资格测试运算符( innot in )通常实现为序列的迭代。 However, container objects can supply the following special method with a more efficient implementation, which also does not require the object be a sequence. 但是,容器对象可以使用更高效的实现提供以下特殊方法,这也不要求对象是序列。

object.__contains__(self, item)

Called to implement membership test operators. 被称为实施会员测试运营商。 Should return true if item is in self, false otherwise. 如果item在self中,则返回true,否则返回false。 For mapping objects, this should consider the keys of the mapping rather than the values or the key-item pairs. 对于映射对象,这应该考虑映射的键而不是值或键 - 项对。

For objects that don't define __contains__() , the membership test first tries iteration via __iter__() , then the old sequence iteration protocol via __getitem__() , see this section in the language reference. 对于未定义__contains__() ,成员资格测试首先通过__iter__()进行迭代,然后通过__getitem__()进行旧的序列迭代协议,请参阅语言参考中的此部分。

So, by default Python iterates over a sequence to implement the in operator. 因此,默认情况下,Python迭代序列以实现in运算符。 If an object defines the __contains__ method, Python uses it instead of iterating. 如果一个对象定义了__contains__方法,那么Python使用它而不是迭代。 So what happens in the __contains__ method? 那么__contains__方法会发生什么? To know exactly, you would have to browse the source . 要确切地知道,您必须浏览源代码 But I can tell you that Python's lists implement __contains__ using iteration. 但我可以告诉你Python的列表使用迭代实现__contains__ Python dictionaries and sets are implemented as hash tables , and therefore support faster membership testing. Python字典和集合实现为哈希表 ,因此支持更快的成员资格测试。

Python's built-in methods are written in the C language - you can see their code by checking out Python's source code yourself. Python的内置方法是用C语言编写的 - 您可以通过自己检查Python的源代码来查看它们的代码。

However, if you want to take a look of an equivalent implementation of all the methods in Python itself, you can check PyPy - which features a Python implementation 100% written in Python and a subset of it (rpython). 但是,如果你想看看Python本身所有方法的等效实现,你可以检查PyPy - 它的特点是100%用Python编写的Python实现及其子集(rpython)。

The in operator calls the __contains__ method in a string object - so you can check for the implementation of the string in both projects - but the actual searching code will be buried deeper. in运算符在字符串对象中调用__contains__方法 - 因此您可以检查两个项目中字符串的实现 - 但实际的搜索代码将被深埋。

Here is some of the code in CPython for it, for example: 以下是CPython中的一些代码,例如:

http://hg.python.org/cpython/file/c310233b1d64/Objects/stringlib/fastsearch.h http://hg.python.org/cpython/file/c310233b1d64/Objects/stringlib/fastsearch.h

You can browse the Python-Source online: http://hg.python.org/ 您可以在线浏览Python-Source: http//hg.python.org/

A good start is to clone the repository you need and then use grep to find the things you need. 一个好的开始是克隆您需要的存储库,然后使用grep来查找您需要的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在pycharm中查看python的内置函数的实现? - How to view the implementation of python's built-in functions in pycharm? 你如何区分numpy数组和Python的内置对象 - how do you distinguish numpy arrays from Python's built-in objects 内置python模块的实现 - Implementation of built-in python modules 为什么Python的内置集合类型的变异方法没有返回任何内容而不是集合本身? - Why do mutating methods for Python's built-in collection types return nothing instead of the collection itself? Python中的子类和内置方法 - Subclassing and built-in methods in Python python的所有内置函数都有魔术方法吗? - Does python have magic methods for all it's built-in functions? Python的内置类型是否有冗余的特殊方法? - Are there redundant special methods on Python's built-in types? 您如何查看内置变量以检查您的答案是否正确? Python - How do you see built-in variables to check if your answer is correct? Python 如何在Python中包装内置方法? (或'如何通过引用传递它们') - How to wrap built-in methods in Python? (or 'how to pass them by reference') 如何不使用内置函数或切片在python中查找字符串的子字符串 - How do you find a substring of a string in python using no built-in function or slicing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM