[Python-ideas] Accepting multiple mappings as positional arguments to create dicts

Guido van Rossum guido at python.org
Thu Apr 12 21:03:15 EDT 2018

It's a slippery slope indeed. While having to change update() alone
wouldn't worry me, the subclass constructors do seem like they are going to
want changing too, and that's indeed a bit much. So let's back off a bit.
Not every three lines of code need a built-in shorthand.

On Thu, Apr 12, 2018 at 8:45 AM, Serhiy Storchaka <storchaka at gmail.com>

> 09.04.18 00:18, Andrés Delfino пише:
>> I thought that maybe dict could accept several mappings as positional
>> arguments, like this:
>>     class Dict4(dict):
>>          def __init__(self, *args, **kwargs):
>>              if len(args) > 1:
>>                  if not all([isinstance(arg, dict) for arg in args]):
>>                      raise TypeError('Dict4 expected instances of dict
>>     since multiple positional arguments were passed')
>>                  temp = args[0].copy()
>>                  for arg in args[1:]:
>>                      temp.update(arg)
>>                  super().__init__(temp, **kwargs)
>>              else:
>>                  super().__init__(*args, **kwargs)
>> AFAIK, this wouldn't create compatibility problems, since you can't pass
>> two positional arguments now anyways.
>> It would be useful to solve the "sum/union dicts" discussion, for
>> example: requests.get(url, params=dict(params, {'foo': bar})
>> Whar are your thoughts?
> It is easy to make the dict constructor merging several positional
> arguments. But this is not a tiny harmless change, it will start a cascade
> of other changes.
> After changing the dict constructor, we will need to update the
> dict.update() method too. Constructors and update() methods of dict
> subclasses (OrderedDict, defaultdict, Counter, and more specialized
> classes) should be updated too. UserDict, WeakKeyDictionary,
> WeakValueDictionary are next. After that we will have a pressure of
> updating constructors and update() methods of abstract classes Mapping and
> MutableMapping. This change will break a lot of third-party code that
> implement concrete implementations of these classes, because adding support
> of new arguments in the method of abstract class breaks an interface.
> We will be able to pass this path (we have already passed it), but we must
> realize how long it is.
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/

--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180412/bcf84128/attachment.html>

More information about the Python-ideas mailing list