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. ...
随机推荐
- DateGridView中添加下拉框列并实现数据绑定、更改背景色
1.添加下拉框 代码实现==> using System; using System.Collections.Generic; using System.Windows.Forms; names ...
- httpd安装
1.软件准备 http://apache.fayea.com/apache-mirror//apr/apr-1.5.1.tar.gz http://apache.fayea.com/apache-mi ...
- 嵌入式应用中CGI编程中POST、GET及环境变量详解
原载地址:http://3633188.blog.51cto.com/3623188/828095 1.POST和GET 一个CGI程序在于服务器之间的信息传输和数据传输一般通过两种方法,即 ...
- CSU 1803 2016(数论)
2016 Problem Description: 给出正整数 n 和 m,统计满足以下条件的正整数对 (a,b) 的数量: 1≤a≤n,1≤b≤m; a×b 是 2016 的倍数. Input: 输 ...
- POJ 2195 Going Home 最小费用流 裸题
给出一个n*m的图,其中m是人,H是房子,.是空地,满足人的个数等于房子数. 现在让每个人都选择一个房子住,每个人只能住一间,每一间只能住一个人. 每个人可以向4个方向移动,每移动一步需要1$,问所有 ...
- MHI ,运动历史图像的的获取[下载自CSDN]
#include "cv.h" #include "highgui.h" #include "stdlib.h" #include &quo ...
- 动态WebService方法
[转] 调用Webservice的方法一般是通过右击项目-->添加服务引用-->输入Webservice地址-->前往-->确定,这样可以顺利调用服,但是需要注意的一点是:如果 ...
- Spring Bean 生命周期2
在spring中,从BeanFactory或ApplicationContext取得的实例为Singleton,也就是预设为每一个Bean的别名只能维持一个实例,而不是每次都产生一个新的对象使用Sin ...
- android studio 安装报错 unable to run mksdcard sdk tool
搜了一下原来缺少这个 sudo apt-get install lib32z1 lib32ncurses5 lib32stdc++6
- 织梦后台更新,报错DedeCMS Error:Tag disabled:"php" more...
网站采用织梦v5.7版本,在做过一次后台补丁更新后,再对网站“生成”操作的时候,无厘头出现报错“ 网站后台--系统--系统基本参数---其他选项 ---模板引擎禁用标签:php ,把php删掉 保存 ...