国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python實現擴展內置類型的方法分析

2020-02-16 10:23:47
字體:
來源:轉載
供稿:網友

本文實例講述了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)            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 铜鼓县| 潢川县| 浮梁县| 扎囊县| 阳曲县| 孝义市| 嘉定区| 郴州市| 苍溪县| 天气| 定兴县| 方正县| 马山县| 文登市| 平远县| 武定县| 宜阳县| 阿拉善左旗| 祥云县| 宜昌市| 康定县| 大同县| 都昌县| 东乌珠穆沁旗| 平利县| 格尔木市| 包头市| 五华县| 东海县| 伽师县| 苍南县| 吉首市| 栖霞市| 雅江县| 麻栗坡县| 德格县| 邓州市| 迭部县| 昌图县| 新丰县| 武强县|