Python实现扩展内置类型的方法分析
作者:Think-througher 发布时间:2021-10-18 02:05:06
标签:Python,扩展
本文实例讲述了Python实现扩展内置类型的方法。分享给大家供大家参考,具体如下:
简介
除了实现新的类型的对象方式外,有时我们也可以通过扩展Python内置类型,从而支持其它类型的数据结构,比如为列表增加队列的插入和删除的方法。本文针对此问题,结合实现集合功能的实例,介绍了扩展Python内置类型的两种方法:通过嵌入内置类型来扩展类型和通过子类方式扩展类型。
通过嵌入内置类型扩展
下面例子通过将list对象作为嵌入类型,实现集合对象,并增加了一下运算符重载。这个类知识包装了Python的列表,以及附加的集合运算。
class Set:
def __init__(self, value=[]): # Constructor
self.data = [] # Manages a list
self.concat(value)
def intersect(self, other): # other is any sequence
res = [] # self is the subject
for x in self.data:
if x in other: # Pick common items
res.append(x)
return Set(res) # Return a new Set
def union(self, other): # other is any sequence
res = self.data[:] # Copy of my list
for x in other: # Add items in other
if not x in res:
res.append(x)
return Set(res)
def concat(self, value): # value: list, Set...
for x in value: # Removes duplicates
if not x in self.data:
self.data.append(x)
def __len__(self): return len(self.data) # len(self)
def __getitem__(self, key): return self.data[key] # self[i]
def __and__(self, other): return self.intersect(other) # self & other
def __or__(self, other): return self.union(other) # self | other
def __repr__(self): return 'Set:' + repr(self.data) # print()
if __name__ == '__main__':
x = Set([1, 3, 5, 7])
print(x.union(Set([1, 4, 7]))) # prints Set:[1, 3, 5, 7, 4]
print(x | Set([1, 4, 6])) # prints Set:[1, 3, 5, 7, 4, 6]
通过子类方式扩展类型
从Python2.2开始,所有内置类型都能直接创建子类,如list,str,dict以及tuple。这样可以让你通过用户定义的class语句,定制或扩展内置类型:建立类型名称的子类并对其进行定制。类型的子类型实例,可用在原始的内置类型能够出现的任何地方。
class Set(list):
def __init__(self, value = []): # Constructor
list.__init__([]) # Customizes list
self.concat(value) # Copies mutable defaults
def intersect(self, other): # other is any sequence
res = [] # self is the subject
for x in self:
if x in other: # Pick common items
res.append(x)
return Set(res) # Return a new Set
def union(self, other): # other is any sequence
res = Set(self) # Copy me and my list
res.concat(other)
return res
def concat(self, value): # value: list, Set . . .
for x in value: # Removes duplicates
if not x in self:
self.append(x)
def __and__(self, other): return self.intersect(other)
def __or__(self, other): return self.union(other)
def __repr__(self): return 'Set:' + list.__repr__(self)
if __name__ == '__main__':
x = Set([1,3,5,7])
y = Set([2,1,4,5,6])
print(x, y, len(x))
print(x.intersect(y), y.union(x))
print(x & y, x | y)
x.reverse(); print(x)
希望本文所述对大家Python程序设计有所帮助。
来源:http://blog.csdn.net/jinguangliu/article/details/44240171
0
投稿
猜你喜欢
- SQL Server 2000中存在的许多的备份和恢复特性都同样保留在了SQL Server 2005中,但是有一些新的提高同样值得我们关注
- asp中使用addnew方法添加一条记录后,我们经常使用取得自递增的ID,而使用bookmark很容易实现这样的功能。rs.open&nbs
- date() 获取日期,格式:2004-2-28 time() 获取时间,格式:22:24:59 now() 获取日期和时间 格式: 200
- 这个是我在蓝色看到的,楼主想实现图片按比例缩放的功能(缩略图),把图片固定在一定的宽高范围内,不会变形,失真。例如:缩略图的框是94px*9
- 一直以来都有这样一个困惑,那就是打开页面间的链接时是在原窗口转换还在新窗口打开呢?如果是在原窗口里转换页面的话,那我还想使用原页面的信息呢?
- 做个性休闲类项目课程材料,对这方面要求多一些,要总结方法、手法、想法等等,头大了;这里总结了一个做个性字体设计的方法,分享一下;方法是比较简
- 本文实例分析了php5.4传引用时报错问题。分享给大家供大家参考,具体如下:php5.3系列版本以及以前版本,传引用没有什么问题,升级到ph
- 本文介绍了ORACLE客户端连服务器的注意事项:1. 通过SQL*NET协议,ORACLE客户端连服务器时一般需要配置sqlnet.ora和
- 这是一段点击复制的代码,现在我的页面里不仅有1个链接需要用到这段代码。请哪位好心人指教一下应该怎么用ID对应的方式来改写这段js,使它实现一
- 获取不带扩展名的文件的名称:import osprintos.path.splitext("path_to_file")
- 本文实例讲述了PHP使用flock实现文件加锁的方法。分享给大家供大家参考。具体分析如下:flock在官方文档里的解释是:flock() 允
- 本文介绍了三种跨域访问的方法,php,asp及jsp种访问远程文件的方法。这几天脑细胞剩下的不多了,不过问题都一个个解决了。我希望搜索引擎能
- 随着现在宽屏显示器的流行,Flash的全屏模式下,越来越需要考虑到普屏显示器与宽屏显示器的差别。Flash全屏模式有以下特点:窗口最大化,且
- 前言大家都知道PHP 的页面静态化有多种实现方式,比如使用输出缓冲(output buffering),该种方式是把数据缓存在 PHP 的缓
- hmac主要应用在身份验证中,它的使用方法是这样的:1. 客户端发出登录请求(假设是浏览器的GET请求)2. 服务器返回一个随机值,并在会话
- 一、问题描述 SQL Plus WorkSheet是一个窗口图形界面的SQL语句编辑器,对于那些喜欢窗口界面而不喜欢字符界面的用户,该工具相
- 占位符说明1、%c,格式化字符及其ASCII码2、%s,格式化字符串3、%d,格式化整数4、%u,格式化无符号整数5、%o,格式化无符号八进
- CSS Sprites技术不新鲜,早在2005年 CSS Zengarden 的园主 Dave Shea 就在 ALA
- 文件操作此为本人学习python过程中的笔记,将持续更新,欢迎提问指正。1.txt文件1.文本文件 txt2.二进制文件 图片视频操作流程打
- MySQL由于它本身的小巧和操作的高效, 在数据库应用中越来越多的被采用.我在开发一个P2P应用的时候曾经使用MySQL来保存P2P节点,由