Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 715
Jac Frall Avatar asked Jun 26 '26 01:06

Jac Frall


1 Answers

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

like image 105
Sergey Ronin Avatar answered Jun 28 '26 14:06

Sergey Ronin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!