【发布时间】: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