I'm tasked with creating a model of a cage of hardware. Each cage contains N slots, each slot may or may not contain a card.
I would like to model the cage using a list. Each list index would correspond to the slot number.
cards.name="Card 0", etc.
This would allow my users to query the model via simple list comprehensions. For example:
for card in cards: print card.name
My users, which are not sophisticated Python users, will be interacting with the model in real-time, so it is not practical to have the list index not correspond to a populated card. In other words, if the user removes a card, I need to do something that will indicate that the card is not populated—my first impulse was to set the list item to
The Bossman likes this scheme, but he's not crazy about the list comprehension above failing if there is a card missing. (Which it currently does.) He's even less supportive of requiring the users to learn enough Python to create list comprehension expressions that will ignore
My thought was to sub-class the
list class, to create a
newclass. It would work exactly like a list, except
for card in cards would only return members not set to
Will someone please demonstrate how to overload the list class so that list comprehensions called on the subclass will ignore
None? (My Python skills have so far begun to break down when I attempt this.)
Can anyone suggest a better approach?