如何在vscode中查找python方法的源代码

Jac*_*all 8 python-3.x visual-studio-code

我正在使用 vscode 并使用了该功能

>>> s = 'hello'
>>> s.capitalize()
'Hello'
Run Code Online (Sandbox Code Playgroud)

我有兴趣查看该函数的源代码,因此我右键单击capitalize并单击go to Definition。这让我看到了builtins.pyi一个似乎是存根文件的地方。它给我的功能是

def capitalize(self) -> str: ...
Run Code Online (Sandbox Code Playgroud)

这并没有太大帮助,所以我在谷歌上搜索了 python 字符串库的源代码并得到了这个

# 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))
Run Code Online (Sandbox Code Playgroud)

在 github 上的以下链接https://github.com/python/cpython/blob/3.7/Lib/string.py

看起来它调用了capitalize,但我似乎找不到这个方法的源代码。这主要只是我无法找到方法/函数的代码的一个例子。我希望在编程时能够快速查看 VScode 的源代码,因为这对我来说是一种很好的学习方式。

我意识到这可能是一件很容易做到的事情,但我一直无法弄清楚。如果有人能指出我正确的方向,我将非常感激。

小智 7

Python 的内置函数(对于 cpython)是用 C 编写的,因此 vscode 向您提供仅显示函数签名的虚拟方法。如果您想查看某些内置方法的源代码,则必须前往包含源代码的 GitHub 页面:

内置函数源码:https://github.com/python/cpython/blob/master/Python/bltinmodule.c

内置类型: https: //github.com/python/cpython/tree/master/Objects