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


本文摘自php中文网,作者黄舟,侵删。

这篇文章主要介绍了Python实现扩展内置类型的方法,结合实例形式分析了Python嵌入内置类型扩展及子类方式扩展的具体实现技巧,需要的朋友可以参考下

本文实例讲述了Python实现扩展内置类型的方法。分享给大家供大家参考,具体如下:

简介

除了实现新的类型的对象方式外,有时我们也可以通过扩展Python内置类型,从而支持其它类型的数据结构,比如为列表增加队列的插入和删除的方法。本文针对此问题,结合实现集合功能的实例,介绍了扩展Python内置类型的两种方法:通过嵌入内置类型来扩展类型和通过子类方式扩展类型。

通过嵌入内置类型扩展

下面例子通过将list对象作为嵌入类型,实现集合对象,并增加了一下运算符重载。这个类知识包装了Python的列表,以及附加的集合运算。


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

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语句,定制或扩展内置类型:建立类型名称的子类并对其进行定制。类型的子类型实例,可用在原始的内置类型能够出现的任何地方。


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

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用什么数据库

Python idle是什么

Python代码缩进和测试模块示例详解

Python语言能做什么的

Python中if语句用法

Python什么时候出的

Python中re模块与正则表达式的介绍(附代码)

Python的gui有哪些

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...