Python str.split的源代码?

Python str.split的源代码?,python,python-2.7,python-internals,Python,Python 2.7,Python Internals,我想看看如何在Python中实现str.split() > inspect.getsource(str.split) TypeError: <method 'split' of 'str' objects> is not a module, class, method, function, traceback, frame, or code object >inspect.getsource(str.split) TypeError:不是模块, 类、方法、函数、回溯、帧或

我想看看如何在Python中实现
str.split()

> inspect.getsource(str.split)

TypeError: <method 'split' of 'str' objects> is not a module, 
class, method, function, traceback, frame, or code object
>inspect.getsource(str.split)
TypeError:不是模块,
类、方法、函数、回溯、帧或代码对象
复制StackOverflow上的另一个示例无效:

inspect.getsource(str.split)
不是用来处理用实现语言编写的代码的(
C
str.split
是内置的,即用
C
编写

实现
str.split
的源代码根据是否提供了
sep
参数分为两部分

第一个函数,用于在未提供任何
sep
参数且split删除空白字符时,为。它是如何实现的是相当直截了当的;主块位于
while
循环中,该循环删除前导空格,如果存在任何空格,则搜索剩余的字符串字符并将其拆分。为了说明这一点,我添加了一些注释:

i=j=0;
而(最大计数-->0){
/*递增计数器,使其超过中的所有前导空格
绳子*/
而(i
类似地,字符作为
sep
提供的情况也是如此。它的实现也非常简单,在看到
split_whitespace
之后再检查一下;你不会觉得太难的


对于分隔符长度超过一个字符的情况,还有一些处理方法。这是通过搜索字符串中的字符并进行相应拆分来实现的。

这是一种内置方法,在编译代码中实现。您必须搜索Python源代码存储库@JrrSape,C++代码看起来很复杂——告诉我很多。谢谢CPython是用英文写的!拆分字符串的实际算法位于
stringlib/split.h