2

I just want to take a look builtin function code. Because I'm a beginner on Python and I think some source code can give me very useful instruction. I made some test code as follows and I did 'Ctrl+click' on 'join' with PyCharm IDE.

zip_command = "zip -r {0} {1}".format(target, ' '.join(source))

And then cursor points builtin.py module's join function, but there is empty code. There is only an explanation. How does this operate? Where is the real code?

def join(self, ab=None, pq=None, rs=None): # real signature unknown; restored from __doc__
    """
    Concatenate any number of strings.

    The string whose method is called is inserted in between each given string.
    The result is returned as a new string.

    Example: '.'.join(['ab', 'pq', 'rs']) -> 'ab.pq.rs'
    """
    pass

'builtin.py' path is : C:\Users\admin.PyCharmCE2019.3\system\python_stubs\542861396\builtins.py

Sam Morgan
  • 2,445
  • 1
  • 16
  • 25
JHLee
  • 41
  • 3
  • 2
    PyCharm is lying to you. `builtins.py` is not a real Python source file; it's something that PyCharm made up. I don't know why they thought this was a good idea. – user2357112 Mar 23 '20 at 00:33
  • 3
    If you're a Python beginner, reading the source code of built-in functions is likely to be more confusing than useful, because all that stuff is actually written in C. – user2357112 Mar 23 '20 at 00:34
  • 1
    @user2357112supportsMonica That should be the accepted answer. Most of the python source is in C. – Rashid 'Lee' Ibrahim Mar 23 '20 at 00:36
  • It might be useful in illustrating a few things and generating appreciation for how programming languages work. Even though I don't know much, I still feel like I learn a bit via osmosis when glancing at Python source, like "oh hey, that sort of makes sense..." etc. Looking under the hood every now and then is good for the soul. – ggorlen Mar 23 '20 at 01:00
  • 2
    Does this answer your question? [Finding the source code for built-in Python functions?](https://stackoverflow.com/questions/8608587/finding-the-source-code-for-built-in-python-functions) – AMC Mar 23 '20 at 01:05
  • @Rashid'Lee'Ibrahim More generally, the type `str` and its methods are built into the Python implementation; the reference and most commonly used version is written in C, but other implementations have existed and can exist, not all of them written in C. – chepner Mar 23 '20 at 02:11
  • @chepner That's true I forgot about Jython, Iron Python, and others like that. I assumed he was talking about the standard official python. – Rashid 'Lee' Ibrahim Mar 23 '20 at 02:26

1 Answers1

1

str.join() is implemented in C, specifically in unicodeobject.c at unicode_join.

"How can I find the source code for builtin functions and objects" doesn't have a great answer. See Finding the source code for built-in Python functions? for some overviews of how CPython is laid out. While some of Python's standard library is written in Python (this sits in lib/), you'll find that builtins and some performance-sensitive components of the standard library have a C implementation. The former resides in objects/, and the latter in modules/.

Brad Solomon
  • 38,521
  • 31
  • 149
  • 235