By defining other special methods, you can specify the behavior of operators on user-defined types. For example, if you define add method for the Time class, you can use the + operator on Time objects.

 def __add__(self,time):
seconds = self.time_to_int() + time.time_to_int()
return Time.int_to_time(seconds)

When you apply the + operator to Time objects, Python invokes __add__. When you print the result, Python invokes __str__. So there is quite a lot happening behind the scenes. Changing the behavior of an operator so that it works with user-defined types is called operator overloading. For every operator in Python there is a corresponding special method, like __add__.

from Thinking in Python

Operator overloading的更多相关文章

  1. [置顶] operator overloading(操作符重载,运算符重载)运算符重载,浅拷贝(logical copy) ,vs, 深拷贝(physical copy)

    operator overloading(操作符重载,运算符重载) 所谓重载就是重新赋予新的意义,之前我们已经学过函数重载,函数重载的要求是函数名相同,函数的参数列表不同(个数或者参数类型).操作符重 ...

  2. Lintcode208 Assignment Operator Overloading (C++ Only) solution 题解

    [题目描述] Implement an assignment operator overloading method. Make sure that: The new data can be copi ...

  3. C# to IL 5 Operator Overloading(操作符重载)

    Every operator overload that we use in C#, gets converted to a function call in IL. Theoverloaded &g ...

  4. [c++] Operator overloading

    c++的操蛋属性:自己为一档,空一档,其他随意. UB_stack a; UB_stack b = a; // copy auto c = a; auto d {a}; // (or auto d = ...

  5. 重载操作符 operator overloading 学习笔记

    重载操作符,只是另外一种调用函数的方法和表现方式,在某些情况它可以让代码更简单易读.注意不要过度使用重载操作符,除非它让你的类更简单,让你的代码更易读. 1语法 如下: 其中友元,关键字不是必须的,但 ...

  6. 面向对象程序设计-C++ Operator Overloading & Type conversion (Static)【第十一次上课笔记】

    本次上课继续讲解了 [ ] .-> 等运算符重载的具体例子 也讲解了C++单个参数的类的类型转换的案例 最后稍微提到了 static 的第三种作用:静态数据成员 具体详解我都已注释出来了,大家可 ...

  7. 面向对象程序设计-C++ Default constructor & Copy constructor& Destructor & Operator Overloading【第九次上课笔记】

    先上笔记内容吧: 这次上课的内容有关 构造函数 析构函数 运算符重载 return * this 内容很细,大家好好回顾笔记再照应程序复习吧 :) #include <iostream> ...

  8. 8 Operator overloading

    在类中,Groovy支持你使用标准的操作符.例如,如果你想使用a+b操作(a和b来自于Z类),那么你在Z类中,必须实现(implement)plus(Zname)方法.

  9. c++ operator

    这篇博文是以前很久写的,贴在我的早期一个blog中,今天google一下,发现还真有不少人转载,可惜并不注明出处.那时觉得operator比较好玩.C++有时它的确是个耐玩的东东.operator它有 ...

随机推荐

  1. label标签的用法

    label 标签for属性 <h1>显式指定通过for(for的值就是对应radio的id的值)</h1> <form> <label for="m ...

  2. ios的runtime为什么可以动态添加方法

    一句话概括 每个instance都有一个isa,这个isa,里面含有所有的方法列表,ios提供库函数增加,修改,即实现了动态添加方法

  3. JavaScript权威指南 第七章 数组

    主要介绍一下数组方法 1.Join() Array.join()方法将数组中所有元素都转换为字符串并连接在一起,返回最后生成的字符串. 可以指定一个可选的字符串在生成的字符串中来分隔数组的各个元素.默 ...

  4. C#(结构体_枚举类型)

        结构体一般定义在Main函数上面,位于Class下面,作为一个类:一般情况Struct定义在Main函数前面,Main函数里面的地方都可以使用,参数前面加上public代表公用变量. 用法 1 ...

  5. centos7配置笔记

    原因:前两天服务器的硬盘出故障,报错:scsi 0:0:2:0: rejecting I/O to dead device,报这个错误的时候重启过一次,撑了一个月时间,现在直接导致整个文件系统崩溃.很 ...

  6. JAVA if条件语句,switch多分支结构,与逻辑运算符

    一.if条件语句 示例: import java.util.Scanner; public class Test{ public static void main(String[] args){ Sc ...

  7. ubuntu14.04 wifi频繁掉线解决

    uname -r sudo lspci -knn sudo lshw -numeric -class network sudo ifconfig -a sudo route -nv sudo rfki ...

  8. 中文unicode范围及unicode编解码

    中文unicode范围 : [\u4e00-\u9fa5] 普通字符串可以用多种方式编码成Unicode字符串,具体要看你究竟选择了哪种编码:unicodestring = u"Hello ...

  9. Tomcat下部署多个项目

    转载地址:http://blog.csdn.net/philosophyatmath/article/details/30246631 同一端口号的多个项目部署     添加: <Context ...

  10. tomcat7 内存溢出 java.lang.OutOfMemoryError: Java heap space

    在{tomcat7_home}bin/catalina.sh最上面添加: JAVA_OPTS="-server -Xms800m -Xmx800m -XX:PermSize=128M -XX ...