[Python-ideas] dict.fromkeys() better as dict().setkeys() ? (and other suggestions)

Josiah Carlson jcarlson at uci.edu
Tue May 29 01:56:17 CEST 2007


Ron Adam <rrr at ronadam.com> wrote:
> There are enough correct uses of it in the wild to keep the behavior, but 
> it can be done in a better way.
> 
> I feel it really should be called set_keys and implemented as a method that 
> operates on the current dictionary instead of being a constructor for a new 
> dictionary.  That will allow you to add keys with a default value to an 
> already existing dictionary, or to create a new one with a dictionary 
> constructor.
> 
>       dict().set_keys(s, v=None)    # The current fromkeys behavior.

The problem with that is that when a method mutates an object, it
shouldn't return the object.  Your new .set_keys() method violates this
behavior that is used in lists, sets, dicts, deques, arrays, etc.

I don't have time to comment on the rest at the moment, will do when I
get a chance.


 - Josiah




More information about the Python-ideas mailing list