Python中的下划线和魔方方法

开发 后端
双下划线一般被用于 Python 内置的特殊方法或者属性,比如 __name__,__file__,有时候也被称之为魔方方法。

[[416081]]

下划线在 Python 中有很特别的意义。

开门见山

下划线在 Python 中有特殊的意义,简单来说,可以总结成三点。

  1. 单下划线在前一般用于声明私有成员,比如 _private_var
  2. 单下划线在后一般用于命名已经被保留关键字占用的变量,比如 class_,type_
  3. 双下划线一般被用于 Python 内置的特殊方法或者属性,比如 __name__,__file__,有时候也被称之为魔方方法。

更多细节的讨论,可以看 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 won't 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.

There's no difference between class, variable, global, etc in these conventions.

有时候我们还能看到就仅仅命名为一个下划线的的变量,这种情况一般是这个变量不重要或者只是一个临时工,连名字都不配拥有。

  1. for _,value in func(): # 假如func每次会返回两个值,我们只关心第二个值 
  2.     use(value) 

思维导图

下面是思维导图的总结

如何调用魔法方法

一些魔法方法直接和内建函数对应,这种情况下,如何调用它们是显而易见的。这有个附录可以作为调用魔法方法的参考。

魔法方法 什么时候被调用 解释
__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
 

如果你还想了解关于魔方方法的更多细节,那么你一定不能错过:

  • 魔法方法指南 [2]
  • Magic method guide[3]

参考资料

[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

责任编辑:武晓燕 来源: 哎妈呀Bug
相关推荐

2023-11-29 18:11:17

Python代码

2023-11-09 08:55:17

Python双下划线

2010-03-04 10:35:51

Python下划线

2021-07-31 19:21:34

Python下划线数值

2021-12-26 22:55:26

Python下划线编程

2020-09-22 09:41:48

Python下划线开发

2013-07-24 19:27:45

iOS开发学习自定义带下划线文本UI

2020-10-24 11:09:35

Python下划线用法

2014-05-08 10:36:59

CSS单词连字符

2022-12-19 15:12:34

python运算符

2021-10-20 07:36:03

Python构造方法

2010-01-20 14:13:33

C++变量

2016-10-27 15:58:35

魔术方法PHP

2009-07-10 14:58:13

JLabel组件JFC和Swing

2010-11-03 10:31:51

PHP魔术方法

2023-11-30 08:21:33

2015-07-22 17:21:34

Oracle数据字典

2019-08-09 09:35:20

JavaScript程序员函数

2018-05-23 14:02:27

2009-12-11 10:44:00

Scala讲座函数 scala
点赞
收藏

51CTO技术栈公众号