1

I want to view specifically only the split() method that is built into Python. I just want to analyze the code that makes the split() method do what it does with strings and the like.

So, is it possible to view how the method was built, and is the code that makes it function even written in the Python language?

TZK203
  • 147
  • 2
  • 9
  • possible duplicate of [How can I get the source code of a Python function?](http://stackoverflow.com/questions/427453/how-can-i-get-the-source-code-of-a-python-function) – avi Oct 18 '14 at 05:38
  • Definitely a duplicate, answered properly. – Terry Jan Reedy Oct 18 '14 at 08:21

2 Answers2

2

You can use inspect, but since you are trying to find source of a built in function, it kinda complicates it. See this answer.

Since Python is open source, you can check the source code of split directly.

Community
  • 1
  • 1
avi
  • 9,292
  • 11
  • 47
  • 84
0
def mysplit(strng, delimiter=" "):
    result=[]
    elm = ""
    if isinstance(strng, str) == False:
        return "This is not string"
        
    for idx, ch in enumerate(strng):
        if(idx == strng.rfind(delimiter)+1):
           elm=strng[idx-1:]
           if(elm != ""):
            result.append(elm)
           elm = ""
        elif(ch != delimiter):
            elm += ch
        else:
            if(elm != ""):
                result.append(elm)
            elm = ""
    return result
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 12 '22 at 01:20
  • This appears to be your own implementation of Python’s split() method within the str module. The question didn’t ask for an alternative implementation—it asked how to view the source code for the existing method. – Matt Turner May 16 '22 at 04:36