Python实现扩展内置类型的方法分析
作者:Think-througher 时间:2021-10-18 02:05:06
本文实例讲述了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
标签:Python,扩展
0
投稿
猜你喜欢
Python批量启动多线程代码实例
2021-09-01 04:41:16
ASP XML制作菜单管理程序
2011-04-04 11:15:00
一篇文章带你学习python的函数与类
2023-10-15 05:04:18
Python实现自动化发送邮件
2023-12-07 14:39:58
优化你的ASP程序及优化网页
2007-10-06 23:02:00
对Python3 goto 语句的使用方法详解
2023-10-14 11:32:49
Python如何使用带有 for 循环的 Lambda 函数
2021-05-28 05:06:05
让字体美起来
2011-06-14 09:50:21
PHP常用函数和常见疑难问题解答
2023-11-08 19:28:17
Django1.7+python 2.78+pycharm配置mysql数据库教程
2023-11-04 14:43:28
网页设计之关于素材和言志
2008-03-23 13:46:00
php封装json通信接口详解及实例
2023-11-14 21:56:26
简单谈谈axios中的get,post方法
2023-10-05 08:47:53
让验证码友好一点
2007-10-20 13:45:00
用PHP+java实现自动新闻滚动窗口
2023-11-22 12:31:01
Python return函数返回值类型和帮助函数使用教程
2021-01-13 14:21:32
ASP 千万级数据分页的存储过程
2011-04-14 11:08:00
Python打印斐波拉契数列实例
2022-11-03 11:40:41
oracle 多个字符替换实现
2009-10-23 17:50:00
基于Python组装jmx并调用JMeter实现压力测试
2023-05-03 04:42:20