Is There A __dunder__ Method Corresponding To |= (pipe Equal/update) For Dicts In Python 3.9?
In python 3.9, dictionaries gained combine | and update |= operators. Is there a dunder/magic method which will enable this to be used for other classes? I've tried looking in the
Solution 1:
Yes, | and |= correspond to __or__ and __ior__.
Don't look at the python source code, look at the documentation. In particular, the data model.
See here
And note, this isn't specific to python 3.9.
Solution 2:
Yes, the method for | is __or__ and the method for |= is __ior__. You can see an (approximate) Python implementation here in PEP 584.
def__or__(self, other):
ifnotisinstance(other, dict):
returnNotImplemented
new = dict(self)
new.update(other)
return new
def__ior__(self, other):
dict.update(self, other)
return self
Solution 3:
No need to dig through the source. It's clearly documented as __or__ and __ior__. https://docs.python.org/3/reference/datamodel.html is the relevant documentation.
Post a Comment for "Is There A __dunder__ Method Corresponding To |= (pipe Equal/update) For Dicts In Python 3.9?"