【问题标题】:How to find source code of python method in vscode如何在vscode中找到python方法的源代码
【发布时间】:2020-01-07 17:56:20
【问题描述】:

我在用vscode,用了函数

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

我对查看函数的源代码很感兴趣,所以我右键单击capitalize 并单击go to Definition。这把我带到了builtins.pyi,这似乎是一个存根文件。它给我的功能是

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

这不是太有帮助,所以我用谷歌搜索了 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))

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

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

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

【问题讨论】:

    标签: python-3.x visual-studio-code


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-28
      • 1970-01-01
      相关资源
      最近更新 更多