[Python-ideas] dict.fromkeys() better as dict().setkeys() ? (and other suggestions)
rrr at ronadam.com
Tue May 29 02:29:07 CEST 2007
Ron Adam 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
>>> 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?
Whoops, Ok I see it now. So the example should be...
d = dict()
I think that would work fine.
>> I don't have time to comment on the rest at the moment, will do when I
>> get a chance.
>> - Josiah
> Ok :-)
> Python-ideas mailing list
> Python-ideas at python.org
More information about the Python-ideas