Python实现扩展内置类型的方法分析

 更新时间:2017年10月16日 11:39:38   作者:Think-througher  
这篇文章主要介绍了Python实现扩展内置类型的方法,结合实例形式分析了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相关内容感兴趣的读者可查看本站专题:《Python编码操作技巧总结》、《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总

希望本文所述对大家Python程序设计有所帮助。

相关文章

  • python画折线图的程序

    python画折线图的程序

    这篇文章主要为大家详细介绍了python画折线图的方法,一个画折线图的程序具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-07-07
  • Python利用WMI实现ping命令的例子

    Python利用WMI实现ping命令的例子

    今天小编就为大家分享一篇Python利用WMI实现ping命令的例子,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-08-08
  • Python递归函数 二分查找算法实现解析

    Python递归函数 二分查找算法实现解析

    这篇文章主要介绍了Python递归函数 二分查找算法实现解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-08-08
  • python中wx模块的具体使用方法

    python中wx模块的具体使用方法

    这篇文章主要介绍了python中wx模块的具体使用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-05-05
  • Python中矩阵创建和矩阵运算方法

    Python中矩阵创建和矩阵运算方法

    今天小编就为大家分享一篇Python中矩阵创建和矩阵运算方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-08-08
  • pytorch实现textCNN的具体操作

    pytorch实现textCNN的具体操作

    这篇文章主要介绍了pytorch实现textCNN的具体操作流程,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-05-05
  • Python cookbook(数据结构与算法)从序列中移除重复项且保持元素间顺序不变的方法

    Python cookbook(数据结构与算法)从序列中移除重复项且保持元素间顺序不变的方法

    这篇文章主要介绍了Python cookbook(数据结构与算法)从序列中移除重复项且保持元素间顺序不变的方法,涉及Python针对列表与字典的元素遍历、判断、去重、排序等相关操作技巧,需要的朋友可以参考下
    2018-03-03
  • TensorFlow tf.nn.conv2d实现卷积的方式

    TensorFlow tf.nn.conv2d实现卷积的方式

    今天小编就为大家分享一篇TensorFlow tf.nn.conv2d实现卷积的方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-01-01
  • Python读写二进制文件的实现

    Python读写二进制文件的实现

    本文主要介绍了Python读写二进制文件的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-04-04
  • Python入门教程(十)Python布尔值介绍

    Python入门教程(十)Python布尔值介绍

    这篇文章主要介绍了Python入门教程(十)Python布尔值,Python是一门非常强大好用的语言,也有着易上手的特性,本文为入门教程,需要的朋友可以参考下
    2023-04-04

最新评论