看了魔法函数,有一点疑问。1中需要用self.word才能执行,而2直接用self就可以执行。而1中Word继承了int基本类型,但在__new__时并没有什么卵用。当用

Word(“123”)来实例化时,看到的运算结果是以字符串形式来进行运算的,比如“123”*3=123123123。

1.

class Word(int):
def __new__(cls, word):
word = int(word)
return int.__new__(cls,word)
def __init__(self,word):
self.word = word def __add__(self, other):
return self.word+other def __sub__(self,other):
return self.word-other def __mul__(self, other):
return self.word*other def __div__(self, other):
return sself.word/other def main():
a=Word(123)
b=Word(12)
print a-b
if __name__ == '__main__':
main()

2.

class Word(str):
def __new__(cls, word):
word = word.replace(" ","")
return str.__new__(cls,word) def __init__(self,word):
self.word = word def __gt__(self, other):
return len(self)>len(other) def __lt__(self,other):
return len(self)<len(other) def __ge__(self, other):
return len(self)>=len(other) def __le__(self, other):
return len(self)<=len(other)
def __eq__(self, other):
return len(self)==len(other) def main():
a=Word("foorrrdd")
b=Word("sswwss")
print a==b
if __name__ == '__main__':
main()

python魔法函数的一些疑问的更多相关文章

  1. python魔法函数__dict__和__getattr__的妙用

    python魔法函数__dict__和__getattr__的妙用 __dict__ __dict__是用来存储对象属性的一个字典,其键为属性名,值为属性的值. 既然__dict__是个字典那么我们就 ...

  2. Python魔法函数

    python中定义的以__开头和结尾的的函数.可以随意定制类的特性.魔法函数定义好之后一般不需要我们自己去调用,而是解释器会自动帮我们调用. __getitem__(self, item) 将类编程一 ...

  3. python魔法函数之__getattr__与__getattribute__

    getattr 在访问对象的属性不存在时,调用__getattr__,如果没有定义该魔法函数会报错 class Test: def __init__(self, name, age): self.na ...

  4. python魔法函数(二)之__getitem__、__len__、__iter__

    魔法函数会增强python类的类型,独立存在 __getitem class Company: def __init__(self, employees): self.employees = empl ...

  5. Python魔法函数与两比特量子系统模拟

    技术背景 本文主要涵盖两个领域的知识点:python的魔法函数和量子计算模拟,我们可以通过一个实际的案例来先审视一下这两个需求是如何被结合起来的. 量子计算模拟背景 ProjectQ是一个非常优雅的开 ...

  6. python 进阶读书笔记2 -- python魔法函数

    #!/usr/bin/env python# -*- coding: utf-8 -*- class student: def __init__(self, name_list): self.stud ...

  7. raindi python魔法函数(一)之__repr__与__str__

    __repr__和__str__都是python中的特殊方法,都是用来输出实例对象的,如果没有定义这两个方法在打印的时候只会输出实例所在的内存地址 这种方式的输出没有可读性,并不能直观的体现实例.py ...

  8. Python的魔法函数系列 __getattrbute__和__getattr__

      #!/usr/bin/env python # -*- coding: utf-8 -*- import sys __metaclass__ = type """ _ ...

  9. python使用魔法函数创建可切片类型

    #!/usr/bin/env python # -*- coding: utf-8 -*- """ 可切片的对象 """ import nu ...

随机推荐

  1. iOS开发之──传感器使用 (转载)

    在实际的应用开发中,会用到传感器,下面首先介绍一下iphone4的传感器,然后对一些传感器的开发的API作一简单介绍. AD:WOT2015 互联网运维与开发者大会 热销抢票 在实际的应用开发中,会用 ...

  2. Xcode 7中http通信出现如下错误:Application Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.

    原因 在iOS9 beta1中,苹果将原http协议改成了https协议,使用 TLS1.2 SSL加密请求数据. 解决方法 编辑 info.plist,加入如下设置: <plist> & ...

  3. iOS 9 使用HTTP的方法

    问题 在ios 9中使用HTTP请求,Xcode就会抛出下面的Exception: App Transport Security has blocked a cleartext HTTP (http: ...

  4. 详解 Spotlight on Unix 监控Linux服务器

    1.安装 Spotlight on Unix 下载地址:http://yunpan.cn/QNWyEEvNS4xc9  访问密码 1c7d 傻瓜安装 2.配置spotlight登陆用户,注意spotl ...

  5. CRM项目经验总结-从DAO层到链接数据池

    IDAO接口 定义项目中所有板块相似功能 也是整个项目的根接口  public interface IDAO {  /**   * 新增数据 @param SQL sql查询语句  @param pa ...

  6. CommonJS的模块规范

    CommonJS对模块的定义十分简单,主要分为模块引用.模块定义和模块标识. 1.模块引用 var math = require('math');//这个方法接受模块标识,以此引入一个模块的API到当 ...

  7. 《Hey程序员 你适合加入创业公司吗?》再补充

    笔者经过多年的走访发现,不是所有优秀的程序员都能在创业公司如鱼得水.根据笔者的经验,具备下面几点优秀品质的程序员会更容易适应创业公司的环境. 1.娴熟的调试技巧可以说,程序员的大部分时间都花在调试程序 ...

  8. 转载 sql 存储过程与函数区别

    SQL Server用户自定义函数和存储过程有类似的功能,都可以创建捆绑SQL语句,存储在server中供以后使用.这样能够极大地提高工作效率,通过以下的各种做法可以减少编程所需的时间: 重复使用编程 ...

  9. C#调用SQL Server分页存储过程

    以SQL Server2012提供的offset ..rows fetch next ..rows only为例 e.g. 表名:Tab1 ------------------------------ ...

  10. python基础(一)

    简单的‘Hello World!’ Python命令行 假设你已经安装好了Python, 那么在Linux命令行输入: $python 将直接进入python.然后在命令行提示符>>> ...