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的更多相关文章

  1. C#关键字base

    例子: public CustomStroke(SharpType type) :base() { this.type = type; } 这里的CustomStroke继承与基类Stroke类,用关 ...

  2. iOS 并行编程:GCD Dispatch Queues

    1 简介 1.1 功能          Grand Central Dispatch(GCD)技术让任务并行排队执行,根据可用的处理资源,安排他们在任何可用的处理器核心上执行任务.任务可以是一个函数 ...

  3. [C#]Base使用小记

    base 关键字用于从派生类中访问基类的成员: • 调用基类上已被其他方法重写的方法. • 指定创建派生类实例时应调用的基类构造函数. 基类访问只能在构造函数.实例方法或实例属性访问器中进行. 从静态 ...

  4. [转] 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 ...

  5. Think Python Glossary

    一.The way of the program problem solving: The process of formulating a problem, finding a solution, a ...

  6. jquery的uploadify上传jsp+servlet

    1.准备材料:下载jquery.uploadify上传js   注意:这个上传在firefox下会出现问题如果你在项目中加了拦截器,因为session会丢失,所以你可以传参的时候带上你所需要的条件,在 ...

  7. Clang之语法抽象语法树AST

    语法分析器的任务是确定某个单词流是否能够与源语言的语法适配,即设定一个称之为上下文无关语言(context-free language)的语言集合,语法分析器建立一颗与(词法分析出的)输入单词流对应的 ...

  8. vue打包速度优化

    这是一个很头疼的问题,webpack极大的简化了前端自动化配置,但是打包速度实在是不如人意.在此之前,本人也尝试过网友的一些方法,但是,很多坑,跳进去就出不来,经过多个项目实践,现总结一下我用到的优化 ...

  9. The Architecture of Open Source Applications: Berkeley DB

    最近研究内存关系数据库的设计与实现,下面一篇为berkeley db原始两位作为的Berkeley DB设计回忆录: Conway's Law states that a design reflect ...

  10. virtual table(有180个评论)

    To implement virtual functions, C++ uses a special form of late binding known as the virtual table. ...

随机推荐

  1. mysql的主从配置以及主主配置

    基础环境 系统:linuxmysql版本:5.5主服务器IP:192.168.1.101从服务器IP:192.168.1.102 1.主服务器(master)要打开二进制日志2.从服务器(slave) ...

  2. 使用eclipse编译调试c++

    1. 安装JDK 到官网下载最新的jdk版本,并安装 http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-18 ...

  3. 不用删除整个Maven本地库文件夹,Eclipse下直接更新Maven库

  4. 有关C,C++,C#, Java的图形图像处理类库 整理(未完待续)

    1.Java相关 1.1 Jzy3D Jzy3D 是一个Java的类库,用来绘制各种各样的三维图形,如下图所示: 下载地址:jzy3d-api,官网 1.2 Proscene 是一个用于创建交互式3D ...

  5. 安装Nginx+uWSGI+Django环境

    Ubuntu Server 12.04 安装Nginx+uWSGI+Django环境 今天要介绍的是利用APT源直接apt-get install安装配置我们所需要的环境,首先按惯例先安装MySQL和 ...

  6. 使ViewStub 来提高UI的加载的性能

    首先看下API中的ViewStub 根据的文档的说明,ViewStub是一种默认不可见的试图,它没有大小,所以不能被改变,也不能通过某些把viewstub添加到布局当中来, 不过我们可以使用infla ...

  7. Hibernae 的延迟加载

    http://blog.csdn.net/xc635960736/article/details/7049863 Hibernae 的延迟加载   Hibernae 的延迟加载是一个非常常用的技术,实 ...

  8. LPC1768之ISP

    一 使用到的芯片内部资源: 1必须是P2.10 2RESET引脚 3必须是串口0 4使用FLASH magic工具. 二操作方法: 先按下reset不放,然后按下ISP,抬起reset,抬起ISP就能 ...

  9. js跳转页面方法整理

    1.window.location.href方式 window.location.href="http://www.zgw8.com"; 2.window.navigate方式跳转 ...

  10. android实现 服务器功能

    package com.weijia.tests; import java.io.IOException; import java.net.InetSocketAddress; import java ...