Newbie class instance tracking system
Alex Martelli
aleaxit at yahoo.com
Thu May 10 07:23:23 EDT 2001
"Lyle Johnson" <ljohnson at resgen.com> wrote in message
news:tfje5p3n0kcefa at corp.supernews.com...
...
> if not Maker.all_cars.has_key(carCompany):
> Maker.all_cars[carCompany] = []
> Maker.all_cars[carCompany].append(carName)
Quite correct! Python 2 lets you code this in a
somewhat different manner, though:
Maker.all_cars.setdefault(carCompany,[]
).append(carName)
I believe the .setdefault() method was added to
dictionaries specifically to enable this kind of
idiom for dictionaries whose values are lists
(it has other uses, of course, but this one does
look like the "typical use-case" in the design).
Alex
More information about the Python-list
mailing list