python重载运算符
2015-09-03 20:45
921 查看
[code]class Time60(object): 'Time60 - trace hours and minutes' def __init__(self,h,m): 'constructor - takes hours and minutes' self.h=h self.m=m def __str__(self): 'string representation' return "%02d:%02d" % (self.h,self.m) __repe__=__str__ def __add__(self,other): 'overloading the addition operator' m=self.m+other.m h=(self.h+other.h+m//60) return self.__class__(h,m%60) def __iadd__(self,other): 'overloading in-place addition' m=self.m+other.m h=(self.h+other.h+m//60) self.m=m%60 self.h=h; return self from random import choice class RandSeq(object): def __init__(self,seq): self.data=seq def __iter__(self): return self def __next__(self): return choice(self.data) x=Time60(12,5) print(x) a=RandSeq([1,2,34,45,7,9,5,3,7]) it=iter(a) for x in range(5): print(it.__next__())
相关文章推荐
- python若干小函数的使用
- python 爬虫获取网站信息(一)
- 欢迎使用CSDN-markdown编辑器
- python内置函数zip
- Python Serial 与STM32J进行串口通讯
- webdriver自动化测试_键盘事件 python
- python学习笔记(-)
- (四)处理列表数据
- 用python写一个温度转换程序
- 系统性能信息模块篇psutil之系统进程管理方法
- 【Python学习日记】 第三天
- python、pip安装
- webdriver简单对象的定位_python
- python中文件读写的快速入门实例
- python2.7学习笔记(9) ——面向对象编程
- python编码问题 decode('unicode-escape')
- python2.7学习笔记(8) ——模块
- IPython基础使用_Round2
- Python的内建常量
- Python的builtins模块