Re: [Python-ideas] Dict joining using + and +=

2019-03-22 Thread Serhiy Storchaka
04.03.19 15:43, Serhiy Storchaka пише: 01.03.19 12:44, Steven D'Aprano пише: On Fri, Mar 01, 2019 at 08:47:36AM +0200, Serhiy Storchaka wrote: Also, if the custom dict subclass implemented the plus operator with different semantic which supports the addition with a dict, this change will break

Re: [Python-ideas] Dict joining using + and +=

2019-03-12 Thread Christopher Barker
> This question is probably on its own a valid argument against the > proposal. When it comes to dicts (and not Mappings in general) {**d1, > **d2} or d.update() already have clearly-defined semantics. Actually, in my mind, this is an argument for an operator (or method) — besides being obtuse,

Re: [Python-ideas] Dict joining using + and +=

2019-03-08 Thread Davide Rizzo
> Counter also uses +/__add__ for a similar behavior. > > >>> c = Counter(a=3, b=1) > >>> d = Counter(a=1, b=2) > >>> c + d # add two counters together: c[x] + d[x] > Counter({'a': 4, 'b': 3}) > > At first I worried that changing base dict would cause confusion for the >

Re: [Python-ideas] Dict joining using + and +=

2019-03-05 Thread Guido van Rossum
On Tue, Mar 5, 2019 at 6:07 PM Raymond Hettinger < raymond.hettin...@gmail.com> wrote: > > > On Mar 5, 2019, at 2:13 PM, Greg Ewing > wrote: > > > > Rhodri James wrote: > >> I have to go and look in the documentation because I expect the union > operator to be '+'. > > > > Anyone raised on

Re: [Python-ideas] Dict joining using + and +=

2019-03-05 Thread Raymond Hettinger
> On Mar 5, 2019, at 2:13 PM, Greg Ewing wrote: > > Rhodri James wrote: >> I have to go and look in the documentation because I expect the union >> operator to be '+'. > > Anyone raised on Pascal is likely to find + and * more > natural. Pascal doesn't have bitwise operators, so it > re-uses

Re: [Python-ideas] Dict joining using + and +=

2019-03-05 Thread Greg Ewing
Rhodri James wrote: I have to go and look in the documentation because I expect the union operator to be '+'. Anyone raised on Pascal is likely to find + and * more natural. Pascal doesn't have bitwise operators, so it re-uses + and * for set operations. I like the economy of this arrangement

Re: [Python-ideas] Dict joining using + and +=

2019-03-05 Thread Rhodri James
On 05/03/2019 09:42, Jimmy Girardet wrote: Indeed the "obscure" argument should be thrown away. The `|` operator in sets seems to be evident for every one on this list but I would be curious to know how many people first got a TypeError doing set1 + set2 and then found set1 | set2 in the doc.

Re: [Python-ideas] Dict joining using + and +=

2019-03-05 Thread Pål Grønås Drange
I just wanted to mention this since it hasn't been brought up, but neither of these work a.keys() + b.keys() a.values() + b.values() a.items() + b.items() However, the following do work: a.keys() | b.keys() a.items() | b.items() Perhaps they work by coincidence (being set types), but I think

Re: [Python-ideas] Dict joining using + and +=

2019-03-05 Thread Serhiy Storchaka
04.03.19 15:29, Serhiy Storchaka пише: Using "|" looks more natural to me than using "+". We should look at discussions for using the "|" operator for sets, if the alternative of using "+" was considered, I think the same arguments for preferring "|" for sets are applicable now for dicts.

Re: [Python-ideas] Dict joining using + and +=

2019-03-05 Thread Steven D'Aprano
On Mon, Mar 04, 2019 at 10:18:13PM -0800, Amber Yust wrote: > Adding the + operator for dictionaries feels like it would be a mistake in > that it offers at most sugar-y benefits, but introduces the significant > drawback of making it easier to introduced unintended errors. What sort of errors?

Re: [Python-ideas] Dict joining using + and +=

2019-03-05 Thread Steven D'Aprano
On Mon, Mar 04, 2019 at 03:33:36PM -0500, Neil Girdhar wrote: > Maybe, but reading through the various replies, it seems that if you > are adding "-" to be analogous to set difference, then the combination > operator should be analogous to set union "|". That's the purpose of this discussion,

Re: [Python-ideas] Dict joining using + and +=

2019-03-05 Thread Inada Naoki
On Tue, Mar 5, 2019 at 6:42 PM Jimmy Girardet wrote: > > Indeed the "obscure" argument should be thrown away. > > The `|` operator in sets seems to be evident for every one on this list > but I would be curious to know how many people first got a TypeError > doing set1 + set2 and then found set1

Re: [Python-ideas] Dict joining using + and +=

2019-03-05 Thread Jimmy Girardet
Indeed the "obscure" argument should be thrown away. The `|` operator in sets seems to be evident for every one on this list but I would be curious to know how many people first got a TypeError doing set1 + set2 and then found set1 | set2 in the doc. Except for math geek the `|` is always

Re: [Python-ideas] Dict joining using + and +=

2019-03-05 Thread Steven D'Aprano
On Mon, Mar 04, 2019 at 09:34:34PM +, Paul Moore wrote: > On Mon, 4 Mar 2019 at 20:42, Guido van Rossum wrote: > > > > Honestly I would rather withdraw the subtraction operators than > > reopen the discussion about making dict more like set. As some people have repeatedly pointed out, we

Re: [Python-ideas] Dict joining using + and +=

2019-03-04 Thread Serhiy Storchaka
04.03.19 21:24, Guido van Rossum пише: * Dicts are not like sets because the ordering operators (<, <=, >, >=) are not defined on dicts, but they implement subset comparisons for sets. I think this is another argument pleading against | as the operator to combine two dicts. Well, I suppose

Re: [Python-ideas] Dict joining using + and +=

2019-03-04 Thread Anders Hovmöller
> Adding the + operator for dictionaries feels like it would be a mistake in > that it offers at most sugar-y benefits, but introduces the significant > drawback of making it easier to introduced unintended errors. I disagree. This argument only really applies to the case "a = a + b", not "a

Re: [Python-ideas] Dict joining using + and +=

2019-03-04 Thread Amber Yust
Adding the + operator for dictionaries feels like it would be a mistake in that it offers at most sugar-y benefits, but introduces the significant drawback of making it easier to introduced unintended errors. This would be the first instance of "addition" where the result can potentially

Re: [Python-ideas] Dict joining using + and +=

2019-03-04 Thread Raymond Hettinger
> On Mar 4, 2019, at 11:24 AM, Guido van Rossum wrote: > > * Regarding how often this is needed, we know that this is proposed and > discussed at length every few years, so I think this will fill a real need. I'm not sure that conclusion follows from the premise :-) Some ideas get

Re: [Python-ideas] Dict joining using + and +=

2019-03-04 Thread Brett Cannon
On Mon, Mar 4, 2019 at 1:29 PM Neil Girdhar wrote: > On Mon, Mar 4, 2019 at 3:58 PM Christopher Barker > wrote: > > > > > > > > On Mon, Mar 4, 2019 at 12:41 PM Guido van Rossum > wrote: > >> > >> Honestly I would rather withdraw the subtraction operators than reopen > the discussion about

Re: [Python-ideas] Dict joining using + and +=

2019-03-04 Thread Dan Sommers
On 3/4/19 5:11 PM, Steven D'Aprano wrote: On Mon, Mar 04, 2019 at 11:56:54AM -0600, Dan Sommers wrote: On 3/4/19 10:44 AM, Steven D'Aprano wrote: > If you know ahead of time which order you want, you can simply reverse > it: > > # prefs = site_defaults + user_defaults + document_prefs >

Re: [Python-ideas] Dict joining using + and +=

2019-03-04 Thread Steven D'Aprano
On Mon, Mar 04, 2019 at 11:56:54AM -0600, Dan Sommers wrote: > On 3/4/19 10:44 AM, Steven D'Aprano wrote: > > > If you know ahead of time which order you want, you can simply reverse > > it: > > > > # prefs = site_defaults + user_defaults + document_prefs > > prefs =

Re: [Python-ideas] Dict joining using + and +=

2019-03-04 Thread Steven D'Aprano
On Sat, Mar 02, 2019 at 11:14:18AM -0800, Raymond Hettinger wrote: > If the existing code were in the form of "d=e.copy(); d.update(f); > d.update(g); d.update(h)", converting it to "d = e + f + g + h" would > be a tempting but algorithmically poor thing to do (because the > behavior is

Re: [Python-ideas] Dict joining using + and +=

2019-03-04 Thread Paul Moore
On Mon, 4 Mar 2019 at 20:42, Guido van Rossum wrote: > > Honestly I would rather withdraw the subtraction operators than reopen the > discussion about making dict more like set. I'm neutral on dict addition, but dict subtraction seemed an odd extension to the proposal. Using b in a - b solely

Re: [Python-ideas] Dict joining using + and +=

2019-03-04 Thread Neil Girdhar
On Mon, Mar 4, 2019 at 3:58 PM Christopher Barker wrote: > > > > On Mon, Mar 4, 2019 at 12:41 PM Guido van Rossum wrote: >> >> Honestly I would rather withdraw the subtraction operators than reopen the >> discussion about making dict more like set. I think that's unfortunate. > > > +1 > > I

Re: [Python-ideas] Dict joining using + and +=

2019-03-04 Thread Christopher Barker
On Mon, Mar 4, 2019 at 12:41 PM Guido van Rossum wrote: > Honestly I would rather withdraw the subtraction operators than reopen the > discussion about making dict more like set. > +1 I think the "dicts are like more-featured" sets is a math-geek perspective, and unlikely to make things more

Re: [Python-ideas] Dict joining using + and +=

2019-03-04 Thread Guido van Rossum
Honestly I would rather withdraw the subtraction operators than reopen the discussion about making dict more like set. On Mon, Mar 4, 2019 at 12:33 PM Neil Girdhar wrote: > On Mon, Mar 4, 2019 at 3:22 PM Guido van Rossum wrote: > > > > On Mon, Mar 4, 2019 at 12:12 PM Neil Girdhar > wrote: >

Re: [Python-ideas] Dict joining using + and +=

2019-03-04 Thread Neil Girdhar
On Mon, Mar 4, 2019 at 3:22 PM Guido van Rossum wrote: > > On Mon, Mar 4, 2019 at 12:12 PM Neil Girdhar wrote: >> >> On Mon, Mar 4, 2019 at 2:26 PM Guido van Rossum wrote: >> > >> > * Dicts are not like sets because the ordering operators (<, <=, >, >=) >> > are not defined on dicts, but they

Re: [Python-ideas] Dict joining using + and +=

2019-03-04 Thread Guido van Rossum
On Mon, Mar 4, 2019 at 12:12 PM Neil Girdhar wrote: > On Mon, Mar 4, 2019 at 2:26 PM Guido van Rossum wrote: > > > > * Dicts are not like sets because the ordering operators (<, <=, >, >=) > are not defined on dicts, but they implement subset comparisons for sets. I > think this is another

Re: [Python-ideas] Dict joining using + and +=

2019-03-04 Thread Neil Girdhar
On Mon, Mar 4, 2019 at 2:26 PM Guido van Rossum wrote: > > * Dicts are not like sets because the ordering operators (<, <=, >, >=) are > not defined on dicts, but they implement subset comparisons for sets. I think > this is another argument pleading against | as the operator to combine two >

Re: [Python-ideas] Dict joining using + and +=

2019-03-04 Thread Guido van Rossum
* Dicts are not like sets because the ordering operators (<, <=, >, >=) are not defined on dicts, but they implement subset comparisons for sets. I think this is another argument pleading against | as the operator to combine two dicts. * Regarding how to construct the new set in __add__, I now

Re: [Python-ideas] Dict joining using + and +=

2019-03-04 Thread David Mertz
On Mon, Mar 4, 2019, 11:45 AM Steven D'Aprano wrote: > > Like other folks in the thread, I also want to merge dicts three times > per > > year. > > I'm impressed that you have counted it with that level of accuracy. Is it > on the same three days each year, or do they move about? *wink* > To be

Re: [Python-ideas] Dict joining using + and +=

2019-03-04 Thread Dan Sommers
On 3/4/19 10:44 AM, Steven D'Aprano wrote: > If you know ahead of time which order you want, you can simply reverse > it: > > # prefs = site_defaults + user_defaults + document_prefs > prefs = dict(ChainMap(document_prefs, user_defaults, site_defaults)) > > but that seems a little

Re: [Python-ideas] Dict joining using + and +=

2019-03-04 Thread Steven D'Aprano
On Mon, Mar 04, 2019 at 03:43:48PM +0200, Serhiy Storchaka wrote: > 01.03.19 12:44, Steven D'Aprano пише: > >On Fri, Mar 01, 2019 at 08:47:36AM +0200, Serhiy Storchaka wrote: > > > >>Currently Counter += dict works and Counter + dict is an error. With > >>this change Counter + dict will return a

Re: [Python-ideas] Dict joining using + and +=

2019-03-04 Thread Steven D'Aprano
On Mon, Mar 04, 2019 at 09:42:53AM -0500, David Mertz wrote: > On Mon, Mar 4, 2019, 8:30 AM Serhiy Storchaka wrote: > > > But is merging two dicts a common enough problem that needs introducing > > an operator to solve it? I need to merge dicts maybe not more than one > > or two times by year,

Re: [Python-ideas] Dict joining using + and +=

2019-03-04 Thread David Mertz
On Mon, Mar 4, 2019, 8:30 AM Serhiy Storchaka wrote: > But is merging two dicts a common enough problem that needs introducing > an operator to solve it? I need to merge dicts maybe not more than one > or two times by year, and I am fine with using the update() method. > Perhaps {**d1, **d2} can

Re: [Python-ideas] Dict joining using + and +=

2019-03-04 Thread Serhiy Storchaka
01.03.19 12:44, Steven D'Aprano пише: On Fri, Mar 01, 2019 at 08:47:36AM +0200, Serhiy Storchaka wrote: Currently Counter += dict works and Counter + dict is an error. With this change Counter + dict will return a value, but it will be different from the result of the += operator. That's how

Re: [Python-ideas] Dict joining using + and +=

2019-03-04 Thread INADA Naoki
On Mon, Mar 4, 2019 at 10:29 PM Serhiy Storchaka wrote: > > It is not that I like to add an operator for dict merging, but dicts are > more like sets than sequences: they can not contain duplicated keys and > the size of the result of merging two dicts can be less than the sum of > their sizes.

Re: [Python-ideas] Dict joining using + and +=

2019-03-04 Thread Serhiy Storchaka
01.03.19 21:31, Guido van Rossum пише: On Thu, Feb 28, 2019 at 10:30 PM Serhiy Storchaka > wrote: And this opens a non-easy problem: how to create a mapping of the same type? Not all mappings, and even not all dict subclasses have a copying constructor.

Re: [Python-ideas] Dict joining using + and +=

2019-03-04 Thread Ivan Levkivskyi
On Sat, 2 Mar 2019 at 19:15, Raymond Hettinger wrote: > > > On Mar 1, 2019, at 11:31 AM, Guido van Rossum wrote: > > > > There's a compromise solution for this possible. We already do this for > Sequence and MutableSequence: Sequence does *not* define __add__, but > MutableSequence *does*

Re: [Python-ideas] Dict joining using + and +=

2019-03-03 Thread francismb
On 2/27/19 7:14 PM, MRAB wrote: > Are there any advantages of using '+' over '|'? or '<-' (d1 <- d2) meaning merge priority (overriding policy for equal keys) on the right dict, and may be '->' (d1 -> d2) merge priority on the left dict over '+' (d1 + d2) ? E.g.: >>> d1 = {'a':1, 'b':1 } >>> d2

Re: [Python-ideas] Dict joining using + and +=

2019-03-03 Thread francismb
On 3/2/19 11:11 PM, MRAB wrote: > '<=' is for comparison, less-than-or-equal (in the case of sets, subset, > which is sort of the same kind of thing). Using it for anything else in > Python would be too confusing. Understandable, so the the proposed (meaning) overloading for <= is also too

Re: [Python-ideas] Dict joining using + and +=

2019-03-02 Thread MRAB
On 2019-03-02 22:02, francismb wrote: On 2/27/19 7:14 PM, MRAB wrote: Are there any advantages of using '+' over '|'? or for e.g. '<=' (d1 <= d2) over '+' (d1 + d2) '<=' is for comparison, less-than-or-equal (in the case of sets, subset, which is sort of the same kind of thing). Using it

Re: [Python-ideas] Dict joining using + and +=

2019-03-02 Thread francismb
On 2/27/19 7:14 PM, MRAB wrote: > Are there any advantages of using '+' over '|'? or for e.g. '<=' (d1 <= d2) over '+' (d1 + d2) ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of

Re: [Python-ideas] Dict joining using + and +=

2019-03-02 Thread francismb
On 3/2/19 8:14 PM, Raymond Hettinger wrote: > Lastly, I'm still bugged by use of the + operator for replace-logic instead > of additive-logic. With numbers and lists and Counters, the plus operator > creates a new object where all the contents of each operand contribute to the > result.

Re: [Python-ideas] Dict joining using + and +=

2019-03-02 Thread Raymond Hettinger
> On Mar 1, 2019, at 11:31 AM, Guido van Rossum wrote: > > There's a compromise solution for this possible. We already do this for > Sequence and MutableSequence: Sequence does *not* define __add__, but > MutableSequence *does* define __iadd__, and the default implementation just > calls

Re: [Python-ideas] Dict joining using + and +=

2019-03-01 Thread Greg Ewing
Serhiy Storchaka wrote: And this opens a non-easy problem: how to create a mapping of the same type? That's the responsibility of the class implementing the + operator. There doesn't have to be any guarantee that a subclass of it will automatically return an instance of the subclass (many

Re: [Python-ideas] Dict joining using + and +=

2019-03-01 Thread Guido van Rossum
On Thu, Feb 28, 2019 at 10:30 PM Serhiy Storchaka wrote: > 28.02.19 23:19, Greg Ewing пише: > > Serhiy Storchaka wrote: > >> I do not understand why we discuss a new syntax for dict merging if we > >> already have a syntax for dict merging: {**d1, **d2} (which works with > >> *all* mappings). >

Re: [Python-ideas] Dict joining using + and +=

2019-03-01 Thread Stefan Behnel
Rémi Lapeyre schrieb am 01.03.19 um 16:44: > Le 1 mars 2019 à 16:04:47, Stefan Behnel a écrit: >> I think the behaviour makes sense when you know how it's implemented (keys >> are stored separately from values). > > Is a Python user expected to know the implementation details of all mappings >

Re: [Python-ideas] Dict joining using + and +=

2019-03-01 Thread INADA Naoki
> > OK, thanks for explaining! So more formally speaking, you want to say that > for other examples of '+' in Python > x1 + y == x2 + y if and only if x1 == x2, while for the proposed '+' for > dicts there may be many different x_i such that > x_i + y gives the same result. > It's bit different

Re: [Python-ideas] Dict joining using + and +=

2019-03-01 Thread Stefan Behnel
Eric V. Smith schrieb am 01.03.19 um 15:49: > I understand Inada to be saying that each value on the LHS (as shown above) > affects the result on the RHS. That's the case with addition of ints and > other types, but not so with the proposed dict addition. As he says, the > {a:1} doesn't affect the

Re: [Python-ideas] Dict joining using + and +=

2019-03-01 Thread Ivan Levkivskyi
On Fri, 1 Mar 2019 at 14:50, Eric V. Smith wrote: > On 3/1/2019 9:38 AM, INADA Naoki wrote: > > Sorry, I'm not good at English enough to explain my mental model. > > > > I meant no skip, no ignorance, no throw away. > > > > In case of 1+2=3, both of 1 and 2 are not skipped, ignored or thrown >

Re: [Python-ideas] Dict joining using + and +=

2019-03-01 Thread Stefan Behnel
Rémi Lapeyre schrieb am 01.03.19 um 15:50: > Le 1 mars 2019 à 15:41:52, Stefan Behnel a écrit: > >> Rémi Lapeyre schrieb am 01.03.19 um 15:06: >>> I’m having issues to understand the semantics of d1 + d2. >>> >>> I think mappings are more complicated than sequences it some things >>> seems not

Re: [Python-ideas] Dict joining using + and +=

2019-03-01 Thread Eric V. Smith
On 3/1/2019 9:38 AM, INADA Naoki wrote: Sorry, I'm not good at English enough to explain my mental model. I meant no skip, no ignorance, no throw away. In case of 1+2=3, both of 1 and 2 are not skipped, ignored or thrown away. On the other hand, in case of {a:1, b:2}+{a:2}={a:2, b:2}, I feel

Re: [Python-ideas] Dict joining using + and +=

2019-03-01 Thread Dan Sommers
On 3/1/19 8:19 AM, Ivan Levkivskyi wrote: On Fri, 1 Mar 2019 at 13:48, INADA Naoki wrote: > > > Is that an invariant you expect to apply to other uses of the + > operator? > > py> x = -1 > py> x <= (x + x) > False > > py> [999] <= ([1, 2, 3] + [999]) > False > Please calm down. I meant each

Re: [Python-ideas] Dict joining using + and +=

2019-03-01 Thread Stefan Behnel
Rémi Lapeyre schrieb am 01.03.19 um 15:06: > I’m having issues to understand the semantics of d1 + d2. > > I think mappings are more complicated than sequences it some things > seems not obvious to me. > > What would be OrderedDict1 + OrderedDict2, in which positions would be > the resulting

Re: [Python-ideas] Dict joining using + and +=

2019-03-01 Thread INADA Naoki
Sorry, I'm not good at English enough to explain my mental model. I meant no skip, no ignorance, no throw away. In case of 1+2=3, both of 1 and 2 are not skipped, ignored or thrown away. On the other hand, in case of {a:1, b:2}+{a:2}={a:2, b:2}, I feel {a:1} is skipped, ignored, or thrown away.

Re: [Python-ideas] Dict joining using + and +=

2019-03-01 Thread Neil Girdhar
On Friday, March 1, 2019 at 5:47:06 AM UTC-5, Steven D'Aprano wrote: > > On Fri, Mar 01, 2019 at 08:47:36AM +0200, Serhiy Storchaka wrote: > > > Currently Counter += dict works and Counter + dict is an error. With > > this change Counter + dict will return a value, but it will be different >

Re: [Python-ideas] Dict joining using + and +=

2019-03-01 Thread Rhodri James
On 01/03/2019 14:06, Rémi Lapeyre wrote: I’m having issues to understand the semantics of d1 + d2. That's understandable, clouds of confusion have been raised. As far as I can tell it's pretty straightforward: d = d1 + d2 is equivalent to: >>> d = d1.copy() >>> d.update(d2) All of your

Re: [Python-ideas] Dict joining using + and +=

2019-03-01 Thread Ivan Levkivskyi
On Fri, 1 Mar 2019 at 13:48, INADA Naoki wrote: > > > > > > Is that an invariant you expect to apply to other uses of the + > > operator? > > > > py> x = -1 > > py> x <= (x + x) > > False > > > > py> [999] <= ([1, 2, 3] + [999]) > > False > > > > Please calm down. I meant each type implements

Re: [Python-ideas] Dict joining using + and +=

2019-03-01 Thread Rémi Lapeyre
I’m having issues to understand the semantics of d1 + d2. I think mappings are more complicated than sequences it some things seems not obvious to me. What would be OrderedDict1 + OrderedDict2, in which positions would be the resulting keys, which value would be used if the same key is present

Re: [Python-ideas] Dict joining using + and +=

2019-03-01 Thread INADA Naoki
> > > Is that an invariant you expect to apply to other uses of the + > operator? > > py> x = -1 > py> x <= (x + x) > False > > py> [999] <= ([1, 2, 3] + [999]) > False > Please calm down. I meant each type implements "sum" in semantics of the type, in lossless way. What "lossless" means is

Re: [Python-ideas] Dict joining using + and +=

2019-03-01 Thread INADA Naoki
On Fri, Mar 1, 2019 at 10:10 PM Steven D'Aprano wrote: > > On Fri, Mar 01, 2019 at 08:59:45PM +0900, INADA Naoki wrote: > > I dislike adding more operator overload to builtin types. > > > > str is not commutative, but it satisfies a in (a+b), and b in (a+b). > > There are no loss. > > Is this an

Re: [Python-ideas] Dict joining using + and +=

2019-03-01 Thread Steven D'Aprano
On Fri, Mar 01, 2019 at 09:58:08PM +0900, INADA Naoki wrote: > >>> {1} <= ({1} | {1.0}) > True > >>> {1.0} <= ({1} | {1.0}) > True > > So dict + dict is totally different than set | set. > dict + dict has los at equality level. Is that an invariant you expect to apply to other uses of the +

Re: [Python-ideas] Dict joining using + and +=

2019-03-01 Thread Steven D'Aprano
On Fri, Mar 01, 2019 at 08:59:45PM +0900, INADA Naoki wrote: > I dislike adding more operator overload to builtin types. > > str is not commutative, but it satisfies a in (a+b), and b in (a+b). > There are no loss. Is this an invariant you expect to apply for other classes that support the

Re: [Python-ideas] Dict joining using + and +=

2019-03-01 Thread Chris Angelico
On Fri, Mar 1, 2019 at 11:00 PM INADA Naoki wrote: > > I dislike adding more operator overload to builtin types. > > str is not commutative, but it satisfies a in (a+b), and b in (a+b). > There are no loss. > > In case of dict + dict, it not only sum. There may be loss value. > >{"a":1} +

Re: [Python-ideas] Dict joining using + and +=

2019-03-01 Thread INADA Naoki
I dislike adding more operator overload to builtin types. str is not commutative, but it satisfies a in (a+b), and b in (a+b). There are no loss. In case of dict + dict, it not only sum. There may be loss value. {"a":1} + {"a":2} = ? In case of a.update(b), it's clear that b wins. In case

Re: [Python-ideas] Dict joining using + and +=

2019-03-01 Thread Steven D'Aprano
On Thu, Feb 28, 2019 at 07:40:25AM -0500, James Lu wrote: > I agree with Storchaka here. The advantage of existing dict merge > syntax is that it will cause an error if the object is not a dict or > dict-like object, thus preventing people from doing bad things. What sort of "bad things" are

Re: [Python-ideas] Dict joining using + and +=

2019-03-01 Thread Steven D'Aprano
On Fri, Mar 01, 2019 at 08:47:36AM +0200, Serhiy Storchaka wrote: > Currently Counter += dict works and Counter + dict is an error. With > this change Counter + dict will return a value, but it will be different > from the result of the += operator. That's how list.__iadd__ works too:

Re: [Python-ideas] Dict joining using + and +=

2019-03-01 Thread Ivan Levkivskyi
On Thu, 28 Feb 2019 at 07:18, Serhiy Storchaka wrote: > [...] > > I do not understand why we discuss a new syntax for dict merging if we > already have a syntax for dict merging: {**d1, **d2} (which works with > *all* mappings). Is not this contradicts the Zen? > FWIW there are already three

Re: [Python-ideas] Dict joining using + and +=

2019-02-28 Thread Serhiy Storchaka
01.03.19 06:21, Guido van Rossum пише: On Wed, Feb 27, 2019 at 11:18 PM Serhiy Storchaka > wrote: Counter uses + for a *different* behavior!  >>> Counter(a=2) + Counter(a=3) Counter({'a': 5}) Well, you can see this as a special case. The proposed +

Re: [Python-ideas] Dict joining using + and +=

2019-02-28 Thread Adrien Ricocotam
I really like this idea. It’s not obvious how to deal with key conflicts and I don’t think replacing by the keys of the second dict is that obviously a good behaviour. With the actual merging ({**d1, **d2}) it works the same as when you build a custom dict so it’s usually known by people. If we

Re: [Python-ideas] Dict joining using + and +=

2019-02-28 Thread Serhiy Storchaka
28.02.19 23:19, Greg Ewing пише: Serhiy Storchaka wrote: I do not understand why we discuss a new syntax for dict merging if we already have a syntax for dict merging: {**d1, **d2} (which works with *all* mappings). But that always returns a dict. A '+' operator could be implemented by other

Re: [Python-ideas] Dict joining using + and +=

2019-02-28 Thread fhsxfhsx
Considering potential ambiguity, I suggest `d1.append(d2)` so we can have an additional argument saying `d1.append(d2, mode="some mode that tells how this function behaviours")`. If we are really to have the new syntax `d1 + d2`, I suggest leaving it for `d1.append(d2, mode="strict")` which

Re: [Python-ideas] Dict joining using + and +=

2019-02-28 Thread Steven D'Aprano
On Thu, Feb 28, 2019 at 08:59:30PM -0800, Hasan Diwan wrote: > Do we really need a "+" and a "-" operation on dictionaries? > [dictinstance.update({k:v}) for k,v in dictinstance.items()] does handle > merges already. I don;t think that does what you intended. That merges dictinstance with

Re: [Python-ideas] Dict joining using + and +=

2019-02-28 Thread Hasan Diwan
Do we really need a "+" and a "-" operation on dictionaries? [dictinstance.update({k:v}) for k,v in dictinstance.items()] does handle merges already. And I'm assuming that "-" should return the difference -- set(d1.keys()) - set(d2.keys()), right? -- H -- OpenPGP:

Re: [Python-ideas] Dict joining using + and +=

2019-02-28 Thread Karthikeyan
The PEP should probably also propose d1-d2. > What would be the output of this? Does this return a new dictionary where keys in d2 are removed in d1 like sets? >>> d = dict((i, i) for i in range(5)) >>> e = dict((i, i) for i in range(4, 10)) >>> d {0: 0, 1: 1, 2: 2, 3: 3, 4: 4} >>> e {4: 4, 5:

Re: [Python-ideas] Dict joining using + and +=

2019-02-28 Thread Guido van Rossum
On Wed, Feb 27, 2019 at 11:18 PM Serhiy Storchaka wrote: > 27.02.19 20:48, Guido van Rossum пише: > > > > On Wed, Feb 27, 2019 at 10:42 AM Michael Selik > > > > wrote > The dict subclass > collections.Counter overrides the update method > > for adding values

Re: [Python-ideas] Dict joining using + and +=

2019-02-28 Thread Greg Ewing
Serhiy Storchaka wrote: I do not understand why we discuss a new syntax for dict merging if we already have a syntax for dict merging: {**d1, **d2} (which works with *all* mappings). But that always returns a dict. A '+' operator could be implemented by other mapping types to return a mapping

Re: [Python-ideas] Dict joining using + and +=

2019-02-28 Thread Karthikeyan
Just to add to the discussion this was brought up previously as part of PEP 448 unpacking generalizations that also added {**x, **y} to merge two dicts in Python 3.5. Previous python ideas thread : https://mail.python.org/pipermail/python-ideas/2015-February/031748.html LWN summary :

Re: [Python-ideas] Dict joining using + and +=

2019-02-28 Thread James Lu
I agree with Storchaka here. The advantage of existing dict merge syntax is that it will cause an error if the object is not a dict or dict-like object, thus preventing people from doing bad things. > On Feb 28, 2019, at 2:16 AM, Serhiy Storchaka wrote: > > 27.02.19 20:48, Guido van Rossum

Re: [Python-ideas] Dict joining using + and +=

2019-02-28 Thread Eric V. Smith
On 2/27/2019 2:08 PM, Guido van Rossum wrote: On Wed, Feb 27, 2019 at 11:06 AM João Matos > wrote: Great. Because I don't program in any other language except Python, I can't make the PR (with the C code). Maybe someone who program in C can help?

Re: [Python-ideas] Dict joining using + and +=

2019-02-27 Thread Serhiy Storchaka
27.02.19 20:48, Guido van Rossum пише: On Wed, Feb 27, 2019 at 10:42 AM Michael Selik > wrote > The dict subclass collections.Counter overrides the update method for adding values instead of overwriting values.

Re: [Python-ideas] Dict joining using + and +=

2019-02-27 Thread Brandt Bucher
Here is a working implementation of dictionary addition, for consideration with the PEP: https://bugs.python.org/issue36144 Brandt > On Feb 27, 2019, at 16:07, Guido van Rossum wrote: > > OK, you're it. Please write a PEP for this. > >> On Wed, Feb 27, 2019 at 3:53 PM Steven D'Aprano

Re: [Python-ideas] Dict joining using + and +=

2019-02-27 Thread Guido van Rossum
OK, you're it. Please write a PEP for this. On Wed, Feb 27, 2019 at 3:53 PM Steven D'Aprano wrote: > On Wed, Feb 27, 2019 at 10:34:43AM -0700, George Castillo wrote: > > > > > > The key conundrum that needs to be solved is what to do for `d1 + d2` > when > > > there are overlapping keys. I

Re: [Python-ideas] Dict joining using + and +=

2019-02-27 Thread Steven D'Aprano
On Wed, Feb 27, 2019 at 10:34:43AM -0700, George Castillo wrote: > > > > The key conundrum that needs to be solved is what to do for `d1 + d2` when > > there are overlapping keys. I propose to make d2 win in this case, which is > > what happens in `d1.update(d2)` anyways. If you want it the other

Re: [Python-ideas] Dict joining using + and +=

2019-02-27 Thread Antoine Pitrou
On Wed, 27 Feb 2019 10:48:21 -0800 Guido van Rossum wrote: > > Great, this sounds like a good argument for + over |. The other argument is > that | for sets *is* symmetrical, [...] As much as it can be: >>> {-0.0} | {0.0} {-0.0} >>> {0.0} | {-0.0} {0.0} ;-) Antoine.

Re: [Python-ideas] Dict joining using + and +=

2019-02-27 Thread Brandt Bucher
I’d like to try my hand at implementing this, if nobody else is interested. I should be able to have something up today. Brandt > On Feb 27, 2019, at 11:05, João Matos wrote: > > Hello, > > Great. > Because I don't program in any other language except Python, I can't make the > PR (with the

Re: [Python-ideas] Dict joining using + and +=

2019-02-27 Thread David Mertz
"foo" + "bar" != "bar" + "foo" On Wed, Feb 27, 2019, 12:35 PM George Castillo wrote: > The key conundrum that needs to be solved is what to do for `d1 + d2` when >> there are overlapping keys. I propose to make d2 win in this case, which is >> what happens in `d1.update(d2)` anyways. If you

Re: [Python-ideas] Dict joining using + and +=

2019-02-27 Thread João Matos
Hello, Great. Because I don't program in any other language except Python, I can't make the PR (with the C code). Maybe someone who program in C can help? Best regards, João Matos On 27-02-2019 18:48, Guido van Rossum wrote:

Re: [Python-ideas] Dict joining using + and +=

2019-02-27 Thread Guido van Rossum
On Wed, Feb 27, 2019 at 10:42 AM Michael Selik wrote: > > > On Wed, Feb 27, 2019 at 10:22 AM Anders Hovmöller > wrote: > >> I dislike the asymmetry with sets: >> >> > {1} | {2} >> {1, 2} >> >> To me it makes sense that if + works for dict then it should for set too. >> >> / Anders >> >> > On 27

Re: [Python-ideas] Dict joining using + and +=

2019-02-27 Thread Michael Selik
On Wed, Feb 27, 2019 at 10:22 AM Anders Hovmöller wrote: > I dislike the asymmetry with sets: > > > {1} | {2} > {1, 2} > > To me it makes sense that if + works for dict then it should for set too. > > / Anders > > > On 27 Feb 2019, at 17:25, João Matos wrote: > > > > Hello, > > > > I would like

Re: [Python-ideas] Dict joining using + and +=

2019-02-27 Thread Anders Hovmöller
I dislike the asymmetry with sets: > {1} | {2} {1, 2} To me it makes sense that if + works for dict then it should for set too. / Anders > On 27 Feb 2019, at 17:25, João Matos wrote: > > Hello, > > I would like to propose that instead of using this (applies to Py3.5 and > upwards) >

Re: [Python-ideas] Dict joining using + and +=

2019-02-27 Thread MRAB
On 2019-02-27 17:37, Guido van Rossum wrote: On Wed, Feb 27, 2019 at 9:34 AM George Castillo > wrote: The key conundrum that needs to be solved is what to do for `d1 + d2` when there are overlapping keys. I propose to make d2 win in this case,

Re: [Python-ideas] Dict joining using + and +=

2019-02-27 Thread E. Madison Bray
On Wed, Feb 27, 2019 at 6:35 PM George Castillo wrote: >> >> The key conundrum that needs to be solved is what to do for `d1 + d2` when >> there are overlapping keys. I propose to make d2 win in this case, which is >> what happens in `d1.update(d2)` anyways. If you want it the other way, >>

Re: [Python-ideas] Dict joining using + and +=

2019-02-27 Thread Oleg Broytman
On Wed, Feb 27, 2019 at 09:05:20AM -0800, Guido van Rossum wrote: > On Wed, Feb 27, 2019 at 8:50 AM Rhodri James wrote: > > > On 27/02/2019 16:25, Jo??o Matos wrote: > > > I would like to propose that instead of using this (applies to Py3.5 and > > upwards) > > > dict_a = {**dict_a, **dict_b}

Re: [Python-ideas] Dict joining using + and +=

2019-02-27 Thread Guido van Rossum
On Wed, Feb 27, 2019 at 9:34 AM George Castillo wrote: > The key conundrum that needs to be solved is what to do for `d1 + d2` when >> there are overlapping keys. I propose to make d2 win in this case, which is >> what happens in `d1.update(d2)` anyways. If you want it the other way, >> simply

Re: [Python-ideas] Dict joining using + and +=

2019-02-27 Thread George Castillo
> > The key conundrum that needs to be solved is what to do for `d1 + d2` when > there are overlapping keys. I propose to make d2 win in this case, which is > what happens in `d1.update(d2)` anyways. If you want it the other way, > simply write `d2 + d1`. This would mean that addition, at least

Re: [Python-ideas] Dict joining using + and +=

2019-02-27 Thread Guido van Rossum
On Wed, Feb 27, 2019 at 8:50 AM Rhodri James wrote: > On 27/02/2019 16:25, João Matos wrote: > > I would like to propose that instead of using this (applies to Py3.5 and > upwards) > > dict_a = {**dict_a, **dict_b} > > > > we could use > > dict_a = dict_a + dict_b > > > > or even better > >

Re: [Python-ideas] Dict joining using + and +=

2019-02-27 Thread Rhodri James
On 27/02/2019 16:25, João Matos wrote: Hello, I would like to propose that instead of using this (applies to Py3.5 and upwards) dict_a = {**dict_a, **dict_b} we could use dict_a = dict_a + dict_b or even better dict_a += dict_b While I don't object to the idea of concatenating

[Python-ideas] Dict joining using + and +=

2019-02-27 Thread João Matos
Hello, I would like to propose that instead of using this (applies to Py3.5 and upwards) dict_a = {**dict_a, **dict_b} we could use dict_a = dict_a + dict_b or even better dict_a += dict_b Best regards, João Matos

  1   2   >