dict literals vs dict(**kwds)

bruno de chez modulix en face bruno at modulix.org
Fri May 26 08:25:28 EDT 2006


> Hm, as far as I know shadowing the builtins is discouraged.

*accidentally* shadowing builtins is a common Python gotcha - that's
why it's comon here to raise *warnings* about this.

Explicitly and knowingly rebinding a builtin (or any other name FWIW)
*with a compatible object* is something else. Just google for
"monkey-patching". And yes, it *is* an application of duck-typing.

>In either case, I would guess that for the vast majority of cases the
> builtin dicts are just fine

That's probably why there are so many uses of dict-like objects
(starting with the __dict__ attribute of classes, which is usually a
DictProxy object) and so many helpers to build dict-like objects.

> and  there's no compelling reason for dict(**kwds).

Yes there is : this *is* the ordinary Python syntax - calling a type to
get an instance of it. The dict-litteral syntax is mostly syntactic
sugar.

> Perhaps it's something that should be reconsidered for Py3K

Hopefully not. The fact that you fail to understand why a given feature
exists and how it can be useful is not a reason to ask for arbitrary
restrictions on the language.




More information about the Python-list mailing list