Python中常见运算符重载方法
来源:互联网 发布:mac双语播放器 编辑:程序博客网 时间:2024/06/05 19:01
在类中,对内置对象(例如:整数和列表)所能做的事,几乎都有相应的特殊名称的重载方法。最常见的就是重载类的构造函数(__init__
函数)
下面我们看一个例子,重构列表中+
运算符, 变成只打印: Hello, Python.
# 先看看原list加号运算符>>> lst = list()>>> lst.append(1)>>> lst[1]>>> lst + [2]>>> lst[1, 2]# 重构`+`号运算符方法__add__class A(list): def __add__(self, other): print 'Hello, Python.'>>> newlst = A()>>> newlst.append(1)>>> newlst + [2]Hello, Python.>>> newlst[1]
我们在类A中重载了列表+
号运算符的方法__add__
,它只输出Hello, Python.
,所以当我们类A的实例对象newlst调用+
号运算符时,并没有像lst一样,添加新的成员,而只是输出字符串(这就是重构运算符方法)。
0 0
- Python中常见运算符重载方法
- python运算符方法重载
- python中运算符“重载”
- Python 特殊方法与运算符重载
- python 特殊方法、运算符重载
- C++中运算符重载的方法
- Python运算符重载
- Python运算符重载
- Python运算符重载
- Python运算符重载
- Python运算符重载
- python 重载运算符
- Python运算符重载
- python重载运算符
- python运算符重载
- Python 运算符重载
- Python运算符重载
- Python 运算符重载
- C++多线程
- Kali Linux下安装VMware
- Python Code规范PEP8
- 关于解决js页面中文乱码问题
- 创建 userSettings/Microsoft.SqlServer.Configuration.LandingPage.Properties.Settings 的配置节处理程序时出错: 未能加载文
- Python中常见运算符重载方法
- java -version找不到系统文件java.exe问题解决
- Three Day(1)---switch
- Livepatch —— 免重启给 Ubuntu Linux 内核打关键性安全补丁
- Postman在chorme中的安装和使用
- 贪心算法 赫夫曼编码问题(Huffman)
- java简单二分法理解
- 常用代码集合
- Compact Querieable Representations of raster data