[Python-ideas] Accepting multiple mappings as positional arguments to create dicts
Serhiy Storchaka
storchaka at gmail.com
Thu Apr 12 11:45:51 EDT 2018
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.
More information about the Python-ideas
mailing list