[Python-ideas] The @update operator for dictionaries

Jonathan Fine jfine2358 at gmail.com
Sat Mar 9 11:12:26 EST 2019


 Anders Hovmöller wrote:

> I don't understand what you mean. Can you provide examples that show the state of the dicts before and after and what the syntax would be the equivalent of in current python?

If a.__radd__ exists, then
    a += b
is equivalent to
    a = a.__radd__(b)

Similarly, if a.__iat_update__ exists then
    a @update= b
would be equivalent to
    a = a.__iat_update__(b)

Here's an implementation
    def __iat_update__(self, other):
        self.update(other)
        return self

Thus, 'b' would be unchanged, and 'a' would be the same dictionary as
before, but updated with 'b'.

I hope this helps.
-- 
Jonathan


More information about the Python-ideas mailing list