Thanks for your time and detailed answer (@pf_moore, @MegaIng), I understand the importance of testing the use cases before such a change.
Here is such example from a quick lookup:
numpy/distutils/ccompiler_opt.py
Before:
feature.update({
k:v for k,v in cfeature.items() if k not in feature
})
After:
feature ^= cfeature
Interesting note: during the searches I got an interesting insight regarding a similar feature - the - operator - implementing this operator for dictionaries has tons of open source uses that will make the code simpler and more readable.
Therefore I want to propose adding the - and -= operations for dicts in python:
d1 - d2 = {k:v for k,v in d1.items() if k not in d2}
This operation will be extremely useful, and the largest amount of examples I saw to it is when the rightmost parameter is a dict or a set.
I think that this operation can come to good use also when the second parameter is a set (it is debatable if we should include every possible collection), in the other hand it will make the feature proposal less concise.
Use cases I saw in large open source projects
matplotlib/axes/_axes.py
Before:
return self.plot(
*args, **{k: v for k, v in kwargs.items() if k not in d})
After:
return self.plot(*args, **(kwargs - d))
sqlalchemy/cyextension/collections.pyx
Before:
else:
other = {cy_id(obj): obj for obj in iterable}
result._members = {k: v for k, v in self._members.items() if k not in other}
After:
else:
other = {cy_id(obj): obj for obj in iterable}
result._members = self._members - other
setuptools/dist.py
Before:
metadata_only = set(self._DISTUTILS_UNSUPPORTED_METADATA)
metadata_only -= {"install_requires", "extras_require"}
dist_attrs = {k: v for k, v in attrs.items() if k not in metadata_only}
After:
metadata_only = set(self._DISTUTILS_UNSUPPORTED_METADATA)
metadata_only -= {"install_requires", "extras_require"}
dist_attrs = attrs - metadata_only
TO SUM UP
- The
^ proposed operation has use cases where it will make the code more readable, it is a natural extension of PEP 584, but indeed less useful from the | operation (it is similar to how the ^ in sets is less useful than the other set operations).
- The
- operation for dicts is extremely useful and I believe almost every python developer used a “long” dict comprehension instead of it in the past.
- It is debatable if the operations should be implemented also to enable mixing the dict and set types. For example
dict1 - set1 should be a dictionary and set1 - dict1 should be a set.
- In order to be concise, in my opinion it is better that in the current proposal we do not include the implementation of mixing a dictionary and another type in the same operation.
- Since it seems that the subtraction operation is more useful, it may be worth issuing a PEP for it before the symmetric difference operation, or implement them together in the same PEP.
- What do you say guys? Is there a python core developer in the audience who wants to go with me on the journey to implement the idea?