[Python-ideas] dict.fromkeys() better as dict().setkeys() ? (and other suggestions)
Ron Adam
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
>>> 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?
Whoops, Ok I see it now. So the example should be...
d = dict()
d.set_keys(s, v=none)
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 :-)
>
> Ron
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>
>
More information about the Python-ideas
mailing list