如何阅读Python文档
我正在尝试理解应该如何阅读Python的文档,比如说这个例子:
string.lstrip(s[, chars])
我该怎么理解这个呢?我知道方括号表示可选的,但那个's'是什么意思呢?有没有地方可以解释一下文档是怎么写的?
1 个回答
0
在文档中没有明确说明,但在
string.lstrip(s[, chars])
string
是一个Python模块,它不是任何字符串(比如说,它不能是 "abc"
)。
参数 s
是你想要去掉空白的字符串(例如,它可以是 "abc"
),这个参数是必须的,不可省略。
括号里的参数是 可选的,它会是一个字符串,里面的字符会从原字符串中去掉。
下面是一些调用这个函数的例子:
import string
print string.lstrip("abc", "a") # "bc"
print string.lstrip(" abc") # "abc"
注意:不要和 "abc".lstrip()
混淆。它们是不同的函数,但结果是一样的。另外,看看 @user2357112 的评论。
编辑:我在我的IDE里测试了一下,实际上文档中显示的就是 s
的内容(按 F2 键可以查看):
def lstrip Found at: string
def lstrip(s, chars=None):
"""lstrip(s [,chars]) -> string
Return a copy of the string s with leading whitespace removed.
If chars is given and not None, remove characters in chars instead.
"""
return s.lstrip(chars)
# Strip trailing tabs and spaces