Python collections模塊中的內置模塊:namedtuple
https://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/001411031239400f7181f65f33a4623bc42276a605debf6000python
import collections Card = collections.namedtuple('Card',['rank','suit']) # 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 rank in self.ranks for suit in self.suits] def __len__(self): return len(self._cards) def __getitem__(self,position): return self._cards[position] deck = FrenchDeck() print(len(deck)) print(deck[0])