You might consider making your collection of cards into your own type of collection, which may provide the individual exceptions you require. If you use a dict for your map collection, you can make your own dict with custom behavior as follows:
class CardsCollection(dict): '''A dict-like collection of cards''' def __getitem__(self, key): try:
Now you can simply use your various methods as follows:
def align(self, card_ID, horizontal, vertical): """Aligns the card to the given position.""" card = self._cards[card_ID] card.align(horizontal, vertical) etc. etc.
... just make sure you use your class for your ._cards attribute.
_cards = CardsCollection(card1_ID = card1, card2_ID = card2)
OR:
_cards = CardsCollection({'card1_ID': card1, 'card2_ID': card2})
The best part is that your user interface class is not tied to any custom or unusual interface (i.e. getcard() ) for the object containing the data. The interface here is consistent with the Python data model . Therefore, if for some reason you decide that you want to use the same interface for another class of objects, you used a proven API that will translate almost everything that was written in a way compatible with the Python data model.
source share