下划线在 Python 中有很特别的下划线和意义。 下划线在 Python 中有特殊的魔方意义,简单来说,下划线和可以总结成三点。魔方 更多细节的香港云服务器魔方讨论,可以看 StackOverflow 上的下划线和这个主题:What is the meaning of single and double underscore before an object name?[1]。 __foo__: this is 魔方just a convention, a way for the Python system to use names that wont conflict with user names. _foo: this is just a convention, a way for the programmer to indicate that the variable is private (whatever that means in Python). __foo: this has real meaning: the interpreter replaces this name with _classname__foo as a way to ensure that the name will not overlap with a similar name in another class. No other form of underscores have meaning in the Python world. Theres no difference between class, variable, global, etc in these conventions. 有时候我们还能看到就仅仅命名为一个下划线的的变量,这种情况一般是下划线和这个变量不重要或者只是一个临时工,连名字都不配拥有。魔方 下面是思维导图的源码库总结 一些魔法方法直接和内建函数对应,这种情况下,如何调用它们是显而易见的。这有个附录可以作为调用魔法方法的参考。 魔法方法 什么时候被调用 解释 __new__(cls [,...]) instance = MyClass(arg1, arg2) __new__在实例创建时调用 __init__(self [,...]) instance = MyClass(arg1,arg2) __init__在实例创建时调用 __cmp__(self) self == other, self > other 等 进行比较时调用 __pos__(self) self 一元加法符号 __neg__(self) -self 一元减法符号 __invert__(self) ~self 按位取反 __index__(self) x[self] 当对象用于索引时 __nonzero__(self) bool(self) 对象的布尔值 __getattr__(self, name) self.name #name 不存在 访问不存在的属性 __setattr__(self, name) self.name = val 给属性赋值 __delattr__(self, name) del self.name 删除属性 __getattribute__(self,name) self.name 访问任意属性 __getitem__(self, key) self[key] 使用索引访问某个元素 __setitem__(self, key) self[key] = val 使用索引给某个元素赋值 __delitem__(self, key) del self[key] 使用索引删除某个对象 __iter__(self) for x in self 迭代 __contains__(self, value) value in self, value not in self 使用 in 进行成员测试 __call__(self [,...]) self(args) “调用” 一个实例 __enter__(self) with self as x: with 声明的上下文管理器 __exit__(self, exc, val, trace) with self as x: with 声明的上下文管理器 __getstate__(self) pickle.dump(pkl_file, self) Pickling __setstate__(self) data = pickle.load(pkl_file) Pickling 如果你还想了解关于魔方方法的更多细节,亿华云那么你一定不能错过: 参考资料 [1]What is the meaning of single and double underscore before an object name?: http://stackoverflow.com/questions/1301346/the-meaning-of-a-single-and-a-double-underscore-before-an-object-name-in-python [2]魔法方法指南: http://pyzh.readthedocs.io/en/latest/python-magic-methods-guide.html [3]Magic method guide: http://www.rafekettler.com/magicmethods.html开门见山
思维导图
如何调用魔法方法