dict literals vs dict(**kwds)

George Sakkis george.sakkis at gmail.com
Fri May 26 15:31:09 CEST 2006


bruno de chez modulix en face wrote:

> > 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.

The thing is there are four (at least?) ways to get a dict instance:

In [1]: d1={'name':'mike', 'age':23}

In [2]: d2=dict(d1)

In [3]: d3=dict(**d1)

In [4]: d4=dict(d1.items())

In [5]: d1==d2==d3==d4
Out[5]: True

Talk about "there should be one and preferably one obvious way to do
it".


> > 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.

Perhaps you fail to understand that the given feature is
1) redundant (see above).
2) restricting in a more serious sense: the future addition of optional
keyword arguments that affect the dict's behaviour. Google for "default
dict" or "dictionary accumulator".

George




More information about the Python-list mailing list