亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時(shí)間:2017年10月16日 11:39:38   作者:Think-througher  
這篇文章主要介紹了Python實(shí)現(xiàn)擴(kuò)展內(nèi)置類型的方法,結(jié)合實(shí)例形式分析了Python嵌入內(nèi)置類型擴(kuò)展及子類方式擴(kuò)展的具體實(shí)現(xiàn)技巧,需要的朋友可以參考下

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

簡介

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

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

下面例子通過將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]

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

從Python2.2開始,所有內(nèi)置類型都能直接創(chuàng)建子類,如list,str,dict以及tuple。這樣可以讓你通過用戶定義的class語句,定制或擴(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)

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python編碼操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總

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

相關(guān)文章

  • Django ForeignKey與數(shù)據(jù)庫的FOREIGN KEY約束詳解

    Django ForeignKey與數(shù)據(jù)庫的FOREIGN KEY約束詳解

    這篇文章主要介紹了Django ForeignKey與數(shù)據(jù)庫的FOREIGN KEY約束詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-05-05
  • python刪除過期log文件操作實(shí)例解析

    python刪除過期log文件操作實(shí)例解析

    這篇文章主要介紹了python刪除過期log文件,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-01-01
  • python實(shí)現(xiàn)蒙特卡羅方法教程

    python實(shí)現(xiàn)蒙特卡羅方法教程

    在本篇文章里小編給大家分享了關(guān)于python實(shí)現(xiàn)蒙特卡羅方法和知識(shí)點(diǎn),有需要的朋友們學(xué)習(xí)下。
    2019-01-01
  • python中DataFrame常用的描述性統(tǒng)計(jì)分析方法詳解

    python中DataFrame常用的描述性統(tǒng)計(jì)分析方法詳解

    這篇文章主要介紹了python中DataFrame常用的描述性統(tǒng)計(jì)分析方法詳解,描述性統(tǒng)計(jì)分析是通過圖表或數(shù)學(xué)方法,對(duì)數(shù)據(jù)資料進(jìn)行整理、分析,并對(duì)數(shù)據(jù)的分布狀態(tài)、數(shù)字特征和隨機(jī)變量之間的關(guān)系進(jìn)行估計(jì)和描述的方法,需要的朋友可以參考下
    2023-07-07
  • pandas帶有重復(fù)索引操作方法

    pandas帶有重復(fù)索引操作方法

    今天小編就為大家分享一篇pandas帶有重復(fù)索引操作方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • python utc datetime轉(zhuǎn)換為時(shí)間戳的方法

    python utc datetime轉(zhuǎn)換為時(shí)間戳的方法

    今天小編就為大家分享一篇python utc datetime轉(zhuǎn)換為時(shí)間戳的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • Python批量裁剪圖形外圍空白區(qū)域

    Python批量裁剪圖形外圍空白區(qū)域

    這篇文章主要介紹了Python批量裁剪圖形外圍空白區(qū)域,批量裁剪掉圖片的背景區(qū)域,一般是白色背景,從而減少背景值的干擾和減少存儲(chǔ),下面文章的具體操作內(nèi)容需要的小伙伴可以參考一下
    2022-04-04
  • Python寫代碼的七條重要技巧介紹

    Python寫代碼的七條重要技巧介紹

    大家好,本篇文章主要講的是Python寫代碼的七條重要技巧介紹,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • python基于OpenCV模塊實(shí)現(xiàn)視頻流數(shù)據(jù)切割為圖像幀數(shù)據(jù)(流程分析)

    python基于OpenCV模塊實(shí)現(xiàn)視頻流數(shù)據(jù)切割為圖像幀數(shù)據(jù)(流程分析)

    這篇文章主要介紹了python基于OpenCV模塊實(shí)現(xiàn)視頻流數(shù)據(jù)切割為圖像幀數(shù)據(jù),這里今天主要是實(shí)踐一下視頻流數(shù)據(jù)的預(yù)處理工作,需要的朋友可以參考下
    2022-05-05
  • Python報(bào)錯(cuò):NameError:?name?‘xxx‘?is?not?defined的解決辦法

    Python報(bào)錯(cuò):NameError:?name?‘xxx‘?is?not?defined的解決辦法

    這篇文章主要給大家介紹了關(guān)于Python報(bào)錯(cuò):NameError:?name?‘xxx‘?is?not?defined的解決辦法,文中通過代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2024-06-06

最新評(píng)論