流畅的python笔记1.1

时间:2022-10-18 16:06:29

1.1 一摞python风格的纸牌

首先,创建纸牌类:

#coding=utf-8\
import collections

#利用collections.namedtuple创建一个类,名字叫Card,类中只有属性没有方法,
#含有属性‘rank’,‘suit’
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]

该步骤创建了一个FrenchDeck类,类中的_cards属性为一个列表,列表中的元素为Card类的实例

#创建实例
beer_card = FrenchDeck()
print(len(beer_card))
print(beer_card[0])
print(beer_card[-1])

运行结果:

52
Card(rank='2', suit='spades')
Card(rank='A', suit='hearts')
由于__getitem__方法的存在,可以用random.choice方法来随机抽取,如下:

deck = FrenchDeck()
from random import choice
print(choice(deck))
print(choice(deck))
print(choice(deck))

结果:

Card(rank='5', suit='clubs')
Card(rank='2', suit='hearts')
Card(rank='4', suit='clubs')


可见__getitem__方法并非只可以用于取值,还可以用于与列表属性相关的其他内容中,如切片操作:

deck = FrenchDeck()
print(deck[:3])
print(deck[12::13])#从索引12开始,每隔13张拿一张牌

结果:

[Card(rank='2', suit='spades'), Card(rank='3', suit='spades'), Card(rank='4', suit='spades')]
[Card(rank='A', suit='spades'), Card(rank='A', suit='diamonds'), Card(rank='A', suit='clubs'), Card(rank='A', suit='hearts')]

此外,__getitem__方法还可以使实例变得可迭代:

deck = FrenchDeck()
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')
...

反向迭代亦可:

deck = FrenchDeck()
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')
Card(rank='9', suit='hearts')

in运算符也可以使用:

deck = FrenchDeck()
print(Card('Q','hearts') in deck)
#结果为True


综上所述:

__getitem__方法可以:

1.实例可切片

2.可被random.choice方法调用

3.可以迭代(反向也可以)

4.可使用in运算符