[Python-ideas] dict(default=int)

Eric V. Smith eric at trueblade.com
Wed Mar 8 17:49:38 EST 2017


On 3/8/2017 5:43 PM, Chris Angelico wrote:
> On Thu, Mar 9, 2017 at 9:39 AM, Brice PARENT <contact at brice.xyz> wrote:
>> 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.
>
> There'd still be a pile of special cases. Granted, there aren't going
> to be very many objects that are both callable and iterable, but there
> certainly _can be_, and if one were passed as the first argument, it
> would be ambiguous.
>
> Safer to keep this out of the signature of dict itself.

If we really want to make defaultdict feel more "builtin" (and I don't 
see any reason to do so), I'd suggest adding a factory function:

dict.defaultdict(int)

Similar in spirit to dict.fromkeys(), except of course returning a 
defauldict, not a dict.

Eric.




More information about the Python-ideas mailing list