Python v2.3 and dict.fromkeys() suggestion

Mark J maj64 at hotmail.com
Mon Jun 9 13:33:35 CEST 2003


I think it's really great that dictionary types now provide a (fast)
way to add elements from a sequence.

It seems, however, that this mechanism could be made more general by
letting fromkeys() be a regular method instead of restricting it to a
class method/constructor.  This would allow (quickly) adding elements
from a sequence (perhaps multiple times) *after* the dictionary has
been created.

Besides being more useful, I think this would also be less confusing. 
(What other built-in has such an alternate constructor?)

Also, once people get used to the idea of being able to add sequence
elements to dictionaries, they'll want a regular method to do the same
anyway (better to make the change before release than deal with a
dict.update_fromkeys() or other naming issue to complement a fromkeys
constructor).

One issue, I suppose, would be what to do if an illegal (non-hashable)
element was encountered half-way through the update.  But Python could
check that all elements are hashible prior to updating the dictionary
much faster and less esotericly than a user could.

If the change were to be made and the method was no longer going to be
a constructor, then it may be appropriate to rename the function
"addkeys".

Thanks for the consideration!

Mike




More information about the Python-list mailing list