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

Moshe Zadka Moshe Zadka <mzadka@geocities.com>
Fri, 1 Oct 1999 07:24:37 +0200 (IST)


On Thu, 30 Sep 1999, Michael P. Reilly wrote:

> >>> classify = {}
> >>> for item in inventory:
> ...   key = item.__class__
> ...   if classify.has_key(key):
> ...     classify[key] = classify[key] + 1
> ...   else:
> ...     classify[key] = 1

But a better idiom, both in clarity and efficiency, would be to simulate
Perl's hash semantics and use .get:

classify={}
for item in inventory:
	classify[item.__class__]=classify.get(item.__class__, 0)


--
Moshe Zadka <mzadka@geocities.com>. 
INTERNET: Learn what you know.
Share what you don't.