Type-base dispatch
In the previous section we added two Time objects, but you also might want to add an integer to a Time object. The following is an alternative version of __add__ that checks the type of other and invokes either add_time or increment:
def __add__(self,other):
if (isinstance(other,Time)):
return self.increment(other)
else:
return Time.int_to_time(self.time_to_int() + other)
The built-in function isinstance takes a value and a class object, and returns True if the value is an instance of the class. If other is a Time object, __add__ invokes add_time. Otherwise it assumes that the seconds parameter is a number and invokes increment. This operation is called a type-based dispatch because it dispatches because it dispatches the computation to different methods based on the type of the arguments.
Unfortunately, this implementation of addition is not commutative. If the integer is the first operand. The problem is, instead of asking the Time object to add an integer, Python is asking an integer to add a Time object, and it doesn’t know how to do that. But there is a clever solution for this problem, the radd method, which stands for ‘right-side add’. This method is invoked when a Time object appears on the right side of the + operator.
def __radd__(self,other):
return self.__add__(other)
from Thinking in Python
Type-base dispatch的更多相关文章
- C#关键字base
例子: public CustomStroke(SharpType type) :base() { this.type = type; } 这里的CustomStroke继承与基类Stroke类,用关 ...
- iOS 并行编程:GCD Dispatch Queues
1 简介 1.1 功能 Grand Central Dispatch(GCD)技术让任务并行排队执行,根据可用的处理资源,安排他们在任何可用的处理器核心上执行任务.任务可以是一个函数 ...
- [C#]Base使用小记
base 关键字用于从派生类中访问基类的成员: • 调用基类上已被其他方法重写的方法. • 指定创建派生类实例时应调用的基类构造函数. 基类访问只能在构造函数.实例方法或实例属性访问器中进行. 从静态 ...
- [转] How to dispatch a Redux action with a timeout?
How to dispatch a Redux action with a timeout? Q I have an action that updates notification state of ...
- Think Python Glossary
一.The way of the program problem solving: The process of formulating a problem, finding a solution, a ...
- jquery的uploadify上传jsp+servlet
1.准备材料:下载jquery.uploadify上传js 注意:这个上传在firefox下会出现问题如果你在项目中加了拦截器,因为session会丢失,所以你可以传参的时候带上你所需要的条件,在 ...
- Clang之语法抽象语法树AST
语法分析器的任务是确定某个单词流是否能够与源语言的语法适配,即设定一个称之为上下文无关语言(context-free language)的语言集合,语法分析器建立一颗与(词法分析出的)输入单词流对应的 ...
- vue打包速度优化
这是一个很头疼的问题,webpack极大的简化了前端自动化配置,但是打包速度实在是不如人意.在此之前,本人也尝试过网友的一些方法,但是,很多坑,跳进去就出不来,经过多个项目实践,现总结一下我用到的优化 ...
- The Architecture of Open Source Applications: Berkeley DB
最近研究内存关系数据库的设计与实现,下面一篇为berkeley db原始两位作为的Berkeley DB设计回忆录: Conway's Law states that a design reflect ...
- virtual table(有180个评论)
To implement virtual functions, C++ uses a special form of late binding known as the virtual table. ...
随机推荐
- PHP替换,只替换匹配到的第一个
function str_replace_limit($search, $replace, $subject, $limit=-1) { if (is_array($search)) { foreac ...
- chmod 权限777 是什么意思(Unix和Linux的各种操作系统下)
在Unix和Linux的各种操作系统下,每个文件(文件夹也被看作是文件)都按读.写.运行设定权限.例如我用ls -l命令列文件表时,得到如下输出:-rw-r--r-- 1 bu users 2254 ...
- 几个关于wcf、rest服务的好帖子
//http://blog.csdn.net/fangxing80/article/details/6247297 //http://msdn.microsoft.com/zh-cn/magazin ...
- Oracle11g空表导出方法
今天凌晨在客户现场进行一个Oracle11g的数据库迁移,习惯性的用了exp/imp,然后在新的数据库发现,空表根本没有exp出来,然后查资料,发现了如下信息:[ORACLE 11G在用EXPORT导 ...
- oracle学习笔记(二)设置归档模式
设置归档模式(mount状态) ALTER database ARCHIVELOG; //关闭数据库 shutdown immediate //启动数据库到mount状态 startup mount ...
- Builder模式(建造者模式)
在设计模式中对Builder模式的定义是用于构建复杂对象的一种模式,所构建的对象往往需要多步初始化或赋值才能完成.那么,在实际的开发过程中,我们哪些地方适合用到Builder模式呢?其中使用Build ...
- PLSQL_性能优化系列14_Oracle High Water Level高水位分析
2014-10-04 Created By BaoXinjian 一.摘要 PLSQL_性能优化系列14_Oracle High Water Level高水位分析 高水位线好比水库中储水的水位线,用于 ...
- NeHe OpenGL教程 第九课:移动图像
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- 【jQuery】关于选择器中的 :first 、 :first-child 、 :first-of-type
[:first] <!DOCTYPE html><html lang="zh-CN"><head> <title>test&l ...
- Python多个版本安装!
Python可以同时安装多个版本,目前我安装的是3.6和3.5,在Eclipse中使用3.6:在Visual Studio中使用3.5.如何让哪个版本的Python成为系统默认的解释器呢?通过调整不同 ...