[issue23372] defaultdict.fromkeys should accept a callable factory

Gavin Panella report at bugs.python.org
Mon Sep 19 11:48:05 EDT 2016


Gavin Panella added the comment:

It's inconsistent that defaultdict([]) should be rejected:

  >>> defaultdict([])
  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
  TypeError: first argument must be callable or None

but defaultdict.fromkeys([]) is okay:

  >>> defaultdict.fromkeys([])
  defaultdict(None, {})

The constructor signature differs between defaultdict and dict, and defaultdict.fromkeys is an alternate constructor, so it seems reasonable to also change its signature.

Also confusing is that I can call fromkeys on an instance of defaultdict:

  >>> dd = defaultdict(list)
  >>> dd.fromkeys([1])
  defaultdict(None, {1: None})

Instinctively I expect the default_factory to be carried over, even though I realise that would be odd behaviour for Python. If defaultdict.fromkeys were to expect a mandatory default_factory argument there wouldn't be this moment of confusion.

----------
nosy: +allenap

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue23372>
_______________________________________


More information about the Python-bugs-list mailing list