dict literals vs dict(**kwds)

George Sakkis george.sakkis at gmail.com
Wed May 24 16:28:41 CEST 2006


bruno at modulix wrote:

> When all your keys are valid Python identifiers, and you may want to use
> another dict-like instead of the builtin dict. It's easy to replace the
> dict() factory in a function, class, or whole module :
>
> class MyDict(...):
>   # dict-like class
>
> dict = MyDict
>
> then all following calls to dict(**kw) in this namespace will use MyDict
> instead of the builtin's one. Can't do that with dict litterals.

Hm, as far as I know shadowing the builtins is discouraged. This is not
the same as duck typing, when you may define a function like

def foo(dict_like_class):
    return dict_like_class(x=1,y=2).iteritems()

and call it as foo(MyDict)

In either case, I would guess that for the vast majority of cases the
builtin dicts are just fine and  there's no compelling reason for
dict(**kwds). Perhaps it's something that should be reconsidered for
Py3K.

George




More information about the Python-list mailing list