[Python-ideas] dict(default=int)

Brice PARENT contact at brice.xyz
Wed Mar 8 17:39:23 EST 2017


Le 08/03/17 à 23:30, Chris Angelico a écrit :
> On Thu, Mar 9, 2017 at 9:23 AM, Brice PARENT <contact at brice.xyz> wrote:
>> Would those 2 dicts be equal ?
>> d1 = dict(default=5)
>> d2 = {'default': 5}
> Easy to find out:
>
>>>> d1 = dict(default=5)
>>>> d2 = {'default': 5}
>>>> d1 == d2
> True
>
> ChrisA
That's my point... If they are equal, it clearly means that the 
declaration of d1 is *not* declaring a defaultdict right now, and is a 
valid syntax. So the behaviour would have to be changed, which would 
make legacy code erroneous in such a case.

But a possible workaround, is if we used the first positional argument 
of dict() as the default value. As right now it doesn't accept 
positional arguments (or at least if they are not iterable, which 
complicates a bit the thing), we could allow a syntax like :
d = dict([default, ][*args, ]**kwargs)
where default is a callable, *args made of iterables, and kwargs any kwargs.


More information about the Python-ideas mailing list