Newbie class instance tracking system

Alex Martelli aleaxit at
Thu May 10 07:23:23 EDT 2001

"Lyle Johnson" <ljohnson at> wrote in message
news:tfje5p3n0kcefa at
>         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:


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).


More information about the Python-list mailing list