Python中类的特殊方法详解
本文和大家分享的主要是python语言中类的特殊方法相关用法,希望对大家有帮助。
构造序列
1._len_(self)
2._getitem_(self,key)
3._setitem_(self,key,value)
4._delitem_(self,key)
程序演示:
myseq.py
class MySeq:
def __init__(self):
self.lseq = ["I","II","III","IV"]
def __len__(self):
return len(self.lseq)
def __getitem__(self,key):
if 0 <= key < 4:
return self.lseq[key]
if __name__ == '__main__':
m = MySeq()
for i in range(4):
print(m[i])
程序的运行结果为:

构造iter
1._iter_(self)
2._next_(self)
程序演示如下:
class MyIter:
def __init__(self,start,end):
self.count = start
self.end = end
def __iter__(self):
return self
def __next__(self):
if self.count < self.end:
r = self.count
self.count += 1
return r
else:
raise StopIteration
if __name__ == '__main__':
for i in MyIter(1,10):
print(i)
程序的运行结果为:

构造可比较类
1._it_()
2._le_()
3._gt_()
4._ge_()
5._eq_()
6._ne_()
程序演示如下:
mycmp.py
class MyIter:
def __init__(self,start,end):
self.count = start
self.end = end
def __iter__(self):
return self
def __next__(self):
if self.count < self.end:
r = self.count
self.count += 1
return r
else:
raise StopIteration
if __name__ == '__main__':
for i in MyIter(1,10):
print(i)
程序的运行结果为:

构造可运算类
1._add_()
2._sub_()
3._mul_()
4._div_()
程序演示如下:
class Point:
def __init__(self,x,y):
self.x = x
self.y = y
def __add__(self,oth):
return Point(self.x + oth.x , self.y + oth.y)
def info(self):
print(self.x,self.y)
if __name__ == '__main__':
pa = Point(1,2)
pb = Point(3,4)
pc = pa + pb
pc.info()
程序的运行结果为:

原文链接:http://www.maiziedu.com/wiki/python/special/
Python中类的特殊方法详解的更多相关文章
- Python中格式化format()方法详解
Python中格式化format()方法详解 Python中格式化输出字符串使用format()函数, 字符串即类, 可以使用方法; Python是完全面向对象的语言, 任何东西都是对象; 字符串的参 ...
- Python中的魔术方法详解
介绍 在Python中,所有以“__”双下划线包起来的方法,都统称为“Magic Method”,中文称『魔术方法』,例如类的初始化方法 __init__ ,Python中所有的魔术方法均在官方文档中 ...
- Python中的魔术方法详解(双下方法)
介绍 在Python中,所有以“__”双下划线包起来的方法,都统称为“Magic Method”,中文称『魔术方法』,例如类的初始化方法 __init__ ,Python中所有的魔术方法均在官方文档中 ...
- Python之list列表方法详解
# 列表test = [11, 22, 33, 44, 55, 22]# append方法,在test列表末尾增加一个元素# append(self, p_object) 参数:p_object:待添 ...
- Python 字符串的所有方法详解
name = "my name is {name} and my age is {age}" # 首字母大写 name.capitalize() # 统计某个字符的个数 name. ...
- python MethodType方法详解和使用
python 中MethodType方法详解和使用 废话不多说,直接上代码 #!/usr/bin/python # -*-coding:utf-8-*- from types import Metho ...
- Python 字符串方法详解
Python 字符串方法详解 本文最初发表于赖勇浩(恋花蝶)的博客(http://blog.csdn.net/lanphaday),如蒙转载,敬请保留全文完整,切勿去除本声明和作者信息. ...
- Python基础之 urllib模块urlopen()与urlretrieve()的使用方法详解。
Python urllib模块urlopen()与urlretrieve()的使用方法详解 1.urlopen()方法urllib.urlopen(url[, data[, proxies]]) ...
- Python调用C/C++动态链接库的方法详解
Python调用C/C++动态链接库的方法详解 投稿:shichen2014 这篇文章主要介绍了Python调用C/C++动态链接库的方法,需要的朋友可以参考下 本文以实例讲解了Python调用C/C ...
随机推荐
- node crypto md5加密,并解决中文不相同的问题
在用crypto模块时碰到了加密中文不相同的问题,多谢群里面@蚂蚁指定 1:解决中文不同的问题 function md5Pay(str) { str = (new Buffer(str)).toStr ...
- find 找出大文件
找到大文件 find . -type f -size +100M -exec du -smh {} \;
- Web前端工程师成长之路
一.何为Web前端工程师? 前端工程师,也叫Web前端开发工程师.他是随着web发展,细分出来的行业.Web前端开发工程师,主要职责是利用(X)HTML/CSS/JavaScript/D ...
- Android You need to use a Theme.AppCompat theme (or descendant) with this activity.
错误描述为:java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with ...
- thttpd和cgilua安装与运行流程分析
安装 参考如下博文安装thttpd软件 http://blog.csdn.net/21aspnet/article/details/7045845 http://blog.csdn.net/drago ...
- 清除div中内容
$.ajax({ url: "SearchSN.aspx", data: "SN=" + $("#txtS ...
- Tomcat 使用Redis存储Session
Tomcat Redis Session Github 地址. 下载 commons-pool2-2.2.jar,jedis-2.5.2.jar,tomcat-redis-session-manage ...
- 学习OpenCV——BOW特征提取函数(特征点篇)
没日没夜的改论文生活终于要告一段落了,比起改论文,学OpenCV就是一件幸福的事情.OpenCV的发展越来越完善了,已经可以直接使用BOW函数来进行对象分类了. 简单的通过特征点分类的方法: ...
- CocoaPod遇到更新不了的原因
CocoaPods 1.0.1 is available. To update use: `gem install cocoapods` Until we reach version 1.0 the ...
- PSVR开发者需要了解的9件事
1 首先需要了解下PSVR的具体硬件参数:整个产品都包括哪些: 2 如何创造更好的 VR 内容 三种准备:设计.内容.技术 针对PSVR设计:体验范围.跟踪限制.社交屏幕 VR应用和内容:模拟.沟通. ...