Jac Frall
Jac Frall

Reputation: 433

How to find source code of python method in vscode

I am using vscode and used the function

>>> s = 'hello'
>>> s.capitalize()
'Hello'

I was interested in seeing the source code for the function so I right clicked on capitalize and clicked go to Definition. This took me to builtins.pyi which seems to be a stub file. The function it gave me was

def capitalize(self) -> str: ...

This isn't too helpful so I googled source code for python string library and got this

# Capitalize the words in a string, e.g. " aBc  dEf " -> "Abc Def".
def capwords(s, sep=None):
    """capwords(s [,sep]) -> string
    Split the argument into words using split, capitalize each
    word using capitalize, and join the capitalized words using
    join.  If the optional second argument sep is absent or None,
    runs of whitespace characters are replaced by a single space
    and leading and trailing whitespace are removed, otherwise
    sep is used to split and join the words.
    """
    return (sep or ' ').join(x.capitalize() for x in s.split(sep))

at the following link on github https://github.com/python/cpython/blob/3.7/Lib/string.py

It looks like it calls capitalize but I can't seem to find the source code for this method. This is mainly just an example of me not being able to find the code for a method/function. I would like to be able to quickly see the source code from VScode when programming as it is a great way for me to learn.

I realize this may be a very easy thing to do, but I have not been able to figure it out. If someone could point me in the right direction I would really appreciate it.

Upvotes: 8

Views: 3423

Answers (1)

Sergey Ronin
Sergey Ronin

Reputation: 776

Python's built-in functions (for cpython) are written in C, so vscode presents you dummy methods that show only the function signatures. If you'd like to view the source code for some built-in methods, you'll have to proceed to the GitHub page with the source code:

Built-in functions source: https://github.com/python/cpython/blob/master/Python/bltinmodule.c

Built-in types: https://github.com/python/cpython/tree/master/Objects

Upvotes: 7

Related Questions