[Tutor] Ok, take one step back and let you look

Joseph J. Strout joe@strout.net
Thu, 30 Sep 1999 13:31:15 -0700


At 3:12 PM -0500 09/30/99, K P wrote:

>Quick example: 3 books: red, blue, green. All instances of class
>Book. 3 glasses: yellow, purple, aqua, all instances of class
>Glassware. In my inventory, I would like them to appear thus:
>
>Item          Qty
>Book         3
>Glass        3

So you just want to find and count the Book instances, say?  Followed 
by the Glassware instances?  Let's just do books first.  One way to 
do it is with a loop:

books = 0
for item in myInventory:
	if isInstance(item, Book): books = books+1

Or, you could do it using 'filter'...

bookList = filter(lambda i: isInstance(i, Book), myInventory)
qtyBooks = len(bookList)

The key trick here is the 'isInstance' built-in function.

HTH,
-- Joe

,------------------------------------------------------------------.
|    Joseph J. Strout           Biocomputing -- The Salk Institute |
|    joe@strout.net             http://www.strout.net              |
`------------------------------------------------------------------'