现在我用一个非常简单的例子来展示如何实现getitme和len这两个特殊方法,通过这个例子我们也能见识到特殊方法的强大。
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]
首先,我们用collections.namedtuple构建了一个简单的类来表示一张纸牌。namedtuple用以构建只有少数属性但是没有方法的对象。
比如:
>>>beer_card=Card('7','djamonds')
>>>beer_card
Card(rank='7', suit='djamonds')
当然,我们这个例子主要是关注FrenchDeck这个类,它既短小又精悍。首先,它跟任何标准Python集合类型一样,可以用len()函数来查看一叠牌有多少张:
>>>deck = FrenchDeck()
>>>len(deck)
52
从一叠牌中抽取特定的一张纸牌,比如说第一张或最后一张,是很容易的:deck[0]或deck[-1]。这都是由getitem方法提供的:
>>>deck[0]
Card(rank='2', suit='spades')
>>>deck[-1]
Card(rank='A', suit='hearts')
我们需要单独写一个方法用来随机抽取一张纸牌吗?没必要,Python已经内置了从一个序列中随机选出一个元素的函数random.choice,我们直接把它用子啊这一摞纸牌实例上就好:
>>> from random import choice
>>> choice(deck)
Card(rank='K', suit='hearts')
>>> choice(deck)
Card(rank='A', suit='clubs')
>>> choice(deck)
Card(rank='10', suit='clubs')
现在已经可以体会到通过实例特殊方法来利用Python数据模型的两个好处。因为getitem方法把[]操作交给了self._cards列表,所以我们的deck类自动支持切片(slicing)操作。下面列出了查看一摞牌最上面3张和只看牌面是A的牌的操作。其中第二种操作的具体方法是,先抽出索引时12的那张牌,然后每隔13张牌拿1张:
>>> deck[:3]
[Card(rank='2', suit='spades'), Card(rank='3', suit='spades'), Card(rank='4', suit='spades')]
>>> deck[12::13]
[Card(rank='A', suit='spades'), Card(rank='A', suit='diamonds'),
Card(rank='A', suit='clubs'), Card(rank='A', suit='hearts')]
另外,仅仅实现了getitem方法,这一摞牌就变成可迭代的了:
>>> for card in deck:
... print(card)
...
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')
...
反向迭代也没关系:
>>> for card in reversed(deck):
... print(card)
...
Card(rank='A', suit='hearts')
Card(rank='K', suit='hearts')
Card(rank='Q', suit='hearts')
Card(rank='J', suit='hearts')
Card(rank='10', suit='hearts')
...