您好,登錄后才能下訂單哦!
本篇內容介紹了“python中接口、繼承、重載運算符的介紹及用法”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
1. 序列__getitem__
2. __setitem__
3. 抽象基類
4. 不要直接子類化內置類型
5. 繼承順序
6. 重載運算符
如果沒有 __iter__ 和 __contains__ 方法,
Python 會調用 __getitem__ 方法,
設法讓 迭代 和 in 運算符可用
class Foo: def __getitem__(self, pos): return range(0, 30, 10)[pos] f = Foo() print(f[1]) # 10 for i in f: print(i) # 0, 10, 20 # 如果沒有 __iter__ 和 __contains__ 方法, # Python 會調用 __getitem__ 方法, # 設法讓迭代和 in 運算符可用
import collections Card = collections.namedtuple('Card', ['rank', 'suit']) class FrenchDeck: ranks = [str(n) for n in range(2, 11)] + list('JQKA') suits = 'spades diamonds clubs hearts'.split() def __init__(self): self._cards = [Card(rank, suit) for suit in self.suits for rank in self.ranks] def __len__(self): return len(self._cards) def __getitem__(self, position): return self._cards[position] def __str__(self): return ",".join(str(i) for i in self._cards) from random import shuffle deck = FrenchDeck() print(str(deck)) # Card(rank='2', suit='spades'),Card(rank='3', suit='spades'),Card(rank='4', suit='spades'),Card(rank='5', suit='spades'),Card(rank='6', suit='spades'),Card(rank='7', suit='spades'),Card(rank='8', suit='spades'),Card(rank='9', suit='spades'),Card(rank='10', suit='spades'),Card(rank='J', suit='spades'),Card(rank='Q', suit='spades'),Card(rank='K', suit='spades'),Card(rank='A', suit='spades'),Card(rank='2', suit='diamonds'),Card(rank='3', suit='diamonds'),Card(rank='4', suit='diamonds'),Card(rank='5', suit='diamonds'),Card(rank='6', suit='diamonds'),Card(rank='7', suit='diamonds'),Card(rank='8', suit='diamonds'),Card(rank='9', suit='diamonds'),Card(rank='10', suit='diamonds'),Card(rank='J', suit='diamonds'),Card(rank='Q', suit='diamonds'),Card(rank='K', suit='diamonds'),Card(rank='A', suit='diamonds'),Card(rank='2', suit='clubs'),Card(rank='3', suit='clubs'),Card(rank='4', suit='clubs'),Card(rank='5', suit='clubs'),Card(rank='6', suit='clubs'),Card(rank='7', suit='clubs'),Card(rank='8', suit='clubs'),Card(rank='9', suit='clubs'),Card(rank='10', suit='clubs'),Card(rank='J', suit='clubs'),Card(rank='Q', suit='clubs'),Card(rank='K', suit='clubs'),Card(rank='A', suit='clubs'),Card(rank='2', suit='hearts'),Card(rank='3', suit='hearts'),Card(rank='4', suit='hearts'),Card(rank='5', suit='hearts'),Card(rank='6', suit='hearts'),Card(rank='7', suit='hearts'),Card(rank='8', suit='hearts'),Card(rank='9', suit='hearts'),Card(rank='10', suit='hearts'),Card(rank='J', suit='hearts'),Card(rank='Q', suit='hearts'),Card(rank='K', suit='hearts'),Card(rank='A', suit='hearts') shuffle(deck) # TypeError: 'FrenchDeck' object does not support item assignment
加入__setitem__
def __setitem__(self, key, value): self._cards[key] = value
shuffle(deck) print(str(deck)) # 牌被隨機打亂了 # Card(rank='6', suit='hearts'),Card(rank='2', suit='spades'),Card(rank='3', suit='hearts'),Card(rank='A', suit='clubs'),Card(rank='7', suit='clubs'),Card(rank='Q', suit='diamonds'),Card(rank='5', suit='hearts'),Card(rank='6', suit='spades'),Card(rank='5', suit='diamonds'),Card(rank='8', suit='diamonds'),Card(rank='K', suit='hearts'),Card(rank='Q', suit='hearts'),Card(rank='8', suit='hearts'),Card(rank='3', suit='diamonds'),Card(rank='Q', suit='clubs'),Card(rank='A', suit='diamonds'),Card(rank='9', suit='hearts'),Card(rank='7', suit='diamonds'),Card(rank='A', suit='spades'),Card(rank='J', suit='hearts'),Card(rank='3', suit='clubs'),Card(rank='4', suit='hearts'),Card(rank='Q', suit='spades'),Card(rank='10', suit='diamonds'),Card(rank='10', suit='hearts'),Card(rank='K', suit='diamonds'),Card(rank='2', suit='diamonds'),Card(rank='4', suit='spades'),Card(rank='3', suit='spades'),Card(rank='7', suit='hearts'),Card(rank='9', suit='clubs'),Card(rank='J', suit='clubs'),Card(rank='J', suit='diamonds'),Card(rank='10', suit='spades'),Card(rank='4', suit='diamonds'),Card(rank='7', suit='spades'),Card(rank='9', suit='diamonds'),Card(rank='2', suit='hearts'),Card(rank='6', suit='clubs'),Card(rank='K', suit='clubs'),Card(rank='4', suit='clubs'),Card(rank='A', suit='hearts'),Card(rank='K', suit='spades'),Card(rank='2', suit='clubs'),Card(rank='J', suit='spades'),Card(rank='8', suit='spades'),Card(rank='5', suit='spades'),Card(rank='9', suit='spades'),Card(rank='10', suit='clubs'),Card(rank='8', suit='clubs'),Card(rank='6', suit='diamonds'),Card(rank='5', suit='clubs')
或者 在類外打補丁 FrenchDeck.__setitem__ = 函數
def set_card(deck, position, card): deck._cards[position] = card FrenchDeck.__setitem__ = set_card
class Test: def __len__(self): return 24 from collections import abc print(isinstance(Test(), abc.Sized)) # True
只要實現了 __len__() 方法,就可以被 abc.Sized 識別為子類
class AnswerDict(dict): def __getitem__(self, key): return 24 ad = AnswerDict(a="good") print(ad['a']) # 24 d = {} d.update(ad) print(d['a']) # good print(ad) # {'a': 'good'} print(d) # {'a': 'good'}
直接子類化內置類型(如 dict、list 或 str)容易出錯, 因為 內置類型的方法 通常會 忽略用戶覆蓋的方法。
不要子類化內置 類型,用戶自己定義的類 應該繼承 collections 模塊 中的類,例如 UserDict、UserList 和 UserString,這些類做了特殊設計,因 此易于擴展
import collections class AnswerDict2(collections.UserDict): def __getitem__(self, key): return 24 ad = AnswerDict2(a="good") print(ad['a']) # 24 d = {} d.update(ad) print(d['a']) # 24 print(ad) # {'a': 'good'} print(d) # {'a': 24}
多重繼承的同名方法調用,根據類的 __mro__ 屬性順次在類中查找
推薦使用 super() 函數
不可變類型,,一定不能實現就地特殊方法
__iadd__等增量賦值特殊方法 必須返回 self
“python中接口、繼承、重載運算符的介紹及用法”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。