python 中以双下划线的是一些系统定义得名称, 让 python 以更优雅得语法实行一些操作, 本质上还是一些函数和变量, 与其他函数和变量无二.
比如 x.__add__(y) 等价于 x+y
有一些很常见, 有一些可能比较偏, 在这里罗列一下, 做个笔记, 备忘.
x.__contains__(y) 等价于 y in x, 在 list,str, dict,set 等容器中有这个函数
__base__, __bases__, __mro__, 关于类继承和函数查找路径的.
class.__subclasses__(), 返回子类列表
x.__call__(...) == x(...)
x.__cmp__(y) == cmp(x,y)
x.__getattribute__('name') == x.name == getattr(x,'name'), 比__getattr__更早调用
x.__hash__() == hash(x)
x.__sizeof__(), x 在内存中的字节数, x 为 class 得话, 就应该是 x.__basicsize__
x.__delattr__('name') == del x.name
__dictoffset__ attribute tells you the offset to where you find the pointer to the __dict__ object in any instance object that has one. It is in bytes.
__flags__, 返回一串数字, 用来判断该类型能否被序列化 (if it's a heap type), __flags__ & 512
S.__format__, 有些类有用
x.__getitem__(y) == x[y], 相应还有__setitem__, 某些不可修改类型如 set,str 没有__setitem__
x.__getslice__(i, j) == x[i:j], 有个疑问, x='123456789', x[::2], 是咋实现得
__subclasscheck__(), check if a class is subclass
__instancecheck__(), check if an object is an instance
__itemsize__, These fields allow calculating the size in bytes of instances of the type. 0 是可变长度, 非 0 则是固定长度
x.__mod__(y) == x%y, x.__rmod__(y) == y%x
x.__module__ , x 所属模块
x.__mul__(y) == x*y, x.__rmul__(y) == y*x
来源: http://www.bubuko.com/infodetail-2484692.html