python操作符重写,实现+操作符,python操作符,import strin
文章由Byrx.net分享于2019-03-23 07:03:00
python操作符重写,实现+操作符,python操作符,import strin
import stringclass spam: def __init__(self): self.eggs = 1 def __del__(self): pass def __add__(self,other): rt = spam() rt.eggs = self.eggs + other.eggs return rt def __coerce__(self,other): rt = spam() if type(other) == type(rt): return (self,other) elif type(other) == type(''): e = string.atoi(other) rt.eggs = e return(self,rt) else: return None def __radd__(self,other): return self + otherif __name__ == '__main__': a = spam() b = spam() a = a + b print 'a now has', a.eggs, 'eggs' a = a + '24' print 'a now has', a.eggs, 'eggs' a = '24' + a print 'a now has', a.eggs, 'eggs'
相关内容
- Instance Objects,instanceobjects,class Comple
- python中的虚函数,python函数,class Super:
- python代码注释,python注释,# a commenti
- python构造函数,,# def __i
- 输出对象属性,输出对象,class SayMyN
- python实现ftp传输,pythonftp传输,import ftpli
- 获得web页面信息,获得web页面,import sys,
- 获得web页面信息,获得web页面,import sys,
- 获得进程的执行路径和import path,importpath,import os, s
- Python获得系统属性和系统时间,python获得系统属性,im
评论关闭