Pre-PEP: Dictionary accumulator methods

Steven Bethard steven.bethard at gmail.com
Fri Apr 1 04:34:31 EST 2005


Greg Ewing wrote:
> Steven Bethard wrote:
> 
>> py> def defaultdict(*args, **kwargs):
>> ...     defaultfactory, args = args[0], args[1:]
> 
> 
> which can be written more succinctly as
> 
>   def defaultdict(defaultfactory, *args, **kwargs):
>     ...

Not if you want to allow the defaultfactory to be called with a keyword 
argument 'defaultfactory'.  Compare my code:

py> def defaultdict(*args, **kwargs):
...     defaultfactory, args = args[0], args[1:]
...     print defaultfactory, args, kwargs
...
py> defaultdict(dict, defaultfactory=True)
<type 'dict'> () {'defaultfactory': True}

with the code you suggested:

py> def defaultdict(defaultfactory, *args, **kwargs):
...     print defaultfactory, args, kwargs
...
py> defaultdict(dict, defaultfactory=True)
Traceback (most recent call last):
   File "<interactive input>", line 1, in ?
TypeError: defaultdict() got multiple values for keyword argument 
'defaultfactory'

Uncommon, sure, but I'd rather not rule it out if there's no need to.

STeVe



More information about the Python-list mailing list