use*_*156 5 python double-underscore
我是Python语言的新手,在执行以下操作时遇到了这种情况:
help(list)
Run Code Online (Sandbox Code Playgroud)
这是我遇到的:
__add__(...)
| x.__add__(y) <==> x+y
|
| __contains__(...)
| x.__contains__(y) <==> y in x
|
| __delitem__(...)
| x.__delitem__(y) <==> del x[y]
Run Code Online (Sandbox Code Playgroud)
关于这些,有哪些下划线?因为当你正常使用一种方法时我们并没有使用它们(据我所知),我很难理解为什么他们会花时间在文档中用下划线写出来.
Don*_*Don 10
有关全面的说明,请参阅Python样式指南.
在实践中:
the following special forms using leading or trailing
underscores are recognized (these can generally be combined with any case
convention):
- _single_leading_underscore: weak "internal use" indicator. E.g. "from M
import *" does not import objects whose name starts with an underscore.
- single_trailing_underscore_: used by convention to avoid conflicts with
Python keyword, e.g.
Tkinter.Toplevel(master, class_='ClassName')
- __double_leading_underscore: when naming a class attribute, invokes name
mangling (inside class FooBar, __boo becomes _FooBar__boo; see below).
- __double_leading_and_trailing_underscore__: "magic" objects or
attributes that live in user-controlled namespaces. E.g. __init__,
__import__ or __file__. Never invent such names; only use them
as documented.
Run Code Online (Sandbox Code Playgroud)
将方法名称用下划线括起来只是分隔命名空间的一种方法。如果一个方法以下划线开头和结尾,则只是一种约定,表明该方法不适合由外部代码使用。
您发布的简介help(list)只是意味着,当您使用 Python 的语法说时e in lst,您实际上是在调用lst.__contains__(e).