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

Josiah Carlson jcarlson at uci.edu
Tue May 29 02:41:33 CEST 2007


Ron Adam <rrr at ronadam.com> wrote:
> Josiah Carlson wrote:
> > 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.
> 
> Huh?  Where does it return an object?

If you are looking to replace dict.fromkeys(...), then dict().set_keys
(...) must return the dictionary that was just created and mutated.


 - Josiah




More information about the Python-ideas mailing list