看了魔法函数,有一点疑问。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. Android程序意外Crash后自动重启

    1.自定义UncaughtExceptionHandler public class UnCeHandler implements UncaughtExceptionHandler { private ...

  2. ARC机制

    ARC概念及原理 1.了解指针分类 (1)强指针:默认的情况下,所有的指针都是强指针,关键字strong (2)弱指针:_ _weak关键字修饰的指针 声明一个弱指针如下: _ _weak Perso ...

  3. 网站错误记录:A transport-level error has occurred when sending the request to the server.

    今天查看公司项目的日志文件,发现有这个错误:A transport-level error has occurred when sending the request to the server. 感 ...

  4. php示例代码之使用list函数和mysql_fetch_row函数

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  5. Java中的内部接口

    什么是内部接口 内部接口也称为嵌套接口,即在一个接口内部定义另一个接口.举个例子,Entry接口定义在Map接口里面,如下代码: public interface Map { interface En ...

  6. Tomcat 的使用学习

    一.Tomcat服务器端口的配置 Tomcat的所有配置都放在conf文件夹之中,里面的server.xml文件是配置的核心文件. 如果想修改Tomcat服务器的启动端口,则可以在server.xml ...

  7. Windows Server 2012 Recycle Bin corrupted

    在Windows Server 2012 上遇到了“The Recycle Bin On E:\ is corrupted. Do you want to empty the Recycle Bin ...

  8. Oracle 列转行函数 Listagg()

    这是最基础的用法: LISTAGG(XXX,XXX) WITHIN GROUP( ORDER BY XXX) 例: select listagg(oeid,',') within GROUP (ord ...

  9. dotNET使用DRPC远程调用运行在Storm上的Topology

    Distributed RPC(DRPC)是Storm构建在Thrift协议上的RPC的实现,DRPC使得你可以通过多种语言远程的使用Storm集群的计算能力.DRPC并非Storm的基础特性,但它确 ...

  10. Iterator(迭代器)的使用

    迭代对于我们搞Java的来说绝对不陌生.我们常常使用JDK提供的迭代接口进行Java集合的迭代. Iterator iterator = list.iterator(); while(iterator ...