[Python-ideas] The @update operator for dictionaries

David Mertz mertz at gnosis.cx
Sat Mar 9 21:06:03 EST 2019


Maybe it's just the C++ IO piping that makes me like it, but these actually
seem intuitive to me, whereas `+` or even `|` leaves me queasy.

On Sat, Mar 9, 2019 at 7:40 PM Ian Foote <ian at feete.org> wrote:

> > It might also be worth considering YAML's own dict merge operator, the
> > "<<" operator, as in https://yaml.org/type/merge.html as this is the
> > existing Python's shift operator added to dict and will require no
> > change to the synatx::
> >
> >   a = a << b
>
>
> I really like this suggestion. It captures the asymmetry, since we could
> have a = a >> b to merge with the other dictionary's keys taking precedence.
>
> My instinct is that a = a << b would take b's values when keys collide and
> a = a >> b would take a's values when keys collide. I'd be very interested
> to know if this matches most peoples' intuitions.
>
> On Sat, 9 Mar 2019 at 18:44, Meitham Jamaa <m at meitham.com> wrote:
>
>> It might also be worth considering YAML's own dict merge operator, the
>> "<<" operator, as in https://yaml.org/type/merge.html as this is the
>> existing Python's shift operator added to dict and will require no
>> change to the synatx::
>>
>>   a = a << b
>>
>> Meitham
>>
>>
>> On 03/10, Chris Angelico wrote:
>> > On Sun, Mar 10, 2019 at 3:16 AM Jonathan Fine <jfine2358 at gmail.com>
>> wrote:
>> > >
>> > >  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'.
>> >
>> > With something this long, how is it better from just writing:
>> >
>> > a = a.update_with(b)
>> >
>> > ? What's the point of an operator, especially if - by your own
>> > statement - it will backward-incompatibly change the language grammar
>> > (in ways that I've yet to understand, since you haven't really been
>> > clear on that)?
>> >
>> > ChrisA
>> >
>>
>> --
>> Meitham Jamaa
>>
>> http://meitham.com
>> GPG Fingerprint: 3934D0B2
>> _______________________________________________
>> 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/
>>
> _______________________________________________
> 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/
>


-- 
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190309/ccb82cb2/attachment.html>


More information about the Python-ideas mailing list