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

首頁(yè) > 編程 > Python > 正文

Python實(shí)現(xiàn)擴(kuò)展內(nèi)置類型的方法分析

2020-01-04 16:35:52
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例講述了Python實(shí)現(xiàn)擴(kuò)展內(nèi)置類型的方法。分享給大家供大家參考,具體如下:

簡(jiǎn)介

除了實(shí)現(xiàn)新的類型的對(duì)象方式外,有時(shí)我們也可以通過(guò)擴(kuò)展Python內(nèi)置類型,從而支持其它類型的數(shù)據(jù)結(jié)構(gòu),比如為列表增加隊(duì)列的插入和刪除的方法。本文針對(duì)此問(wèn)題,結(jié)合實(shí)現(xiàn)集合功能的實(shí)例,介紹了擴(kuò)展Python內(nèi)置類型的兩種方法:通過(guò)嵌入內(nèi)置類型來(lái)擴(kuò)展類型和通過(guò)子類方式擴(kuò)展類型。

通過(guò)嵌入內(nèi)置類型擴(kuò)展

下面例子通過(guò)將list對(duì)象作為嵌入類型,實(shí)現(xiàn)集合對(duì)象,并增加了一下運(yùn)算符重載。這個(gè)類知識(shí)包裝了Python的列表,以及附加的集合運(yùn)算。

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]

通過(guò)子類方式擴(kuò)展類型

從Python2.2開(kāi)始,所有內(nèi)置類型都能直接創(chuàng)建子類,如list,str,dict以及tuple。這樣可以讓你通過(guò)用戶定義的class語(yǔ)句,定制或擴(kuò)展內(nèi)置類型:建立類型名稱的子類并對(duì)其進(jìn)行定制。類型的子類型實(shí)例,可用在原始的內(nèi)置類型能夠出現(xiàn)的任何地方。

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)

 

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到python教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 宾阳县| 龙州县| 巨鹿县| 庆安县| 惠州市| 枣庄市| 吉林省| 陵水| 金秀| 牡丹江市| 海南省| 东光县| 开平市| 富川| 天水市| 平顶山市| 隆子县| 哈巴河县| 卓资县| 孙吴县| 海门市| 大丰市| 昌吉市| 自治县| 佛山市| 弥渡县| 麻栗坡县| 芜湖市| 北票市| 石景山区| 囊谦县| 南阳市| 会东县| 偏关县| 鲁甸县| 施甸县| 福鼎市| 东港市| 馆陶县| 西和县| 增城市|