[Python-ideas] Re: Delete dictionary entry if key exists using -= operator via __isub__()

2022-04-29 Thread Chris Angelico
On Fri, 29 Apr 2022 at 18:39, Greg Ewing wrote: > > On 29/04/22 6:02 am, Zach Victor wrote: > > For me, pop() says "give me something." > > In English the word "pop" doesn't necessarily imply getting > something wanted, or even getting anything at all. Think > popping a champagne cork, or popping

[Python-ideas] Re: Delete dictionary entry if key exists using -= operator via __isub__()

2022-04-29 Thread Greg Ewing
On 29/04/22 6:02 am, Zach Victor wrote: For me, pop() says "give me something." In English the word "pop" doesn't necessarily imply getting something wanted, or even getting anything at all. Think popping a champagne cork, or popping a balloon. -- Greg

[Python-ideas] Re: Delete dictionary entry if key exists using -= operator via __isub__()

2022-04-28 Thread Zach Victor
Agree — it is perhaps a bit strange to use an operator for something other than dictionary-to-dictionary operations. And, yes, benchmarking is the only way to know—but I think your hypothesis is a good one. ___ Python-ideas mailing list --

[Python-ideas] Re: Delete dictionary entry if key exists using -= operator via __isub__()

2022-04-28 Thread Zach Victor
> Can you give an example of real code that does this? In general, parsing JSON data and normalizing it. The existing architecture has microservices consuming PubSub messages for which the data-payload is JSON. In some cases, deleting dictionary keys is convenient (as opposed to, say, building

[Python-ideas] Re: Delete dictionary entry if key exists using -= operator via __isub__()

2022-04-28 Thread Zach Victor
The original idea was actually not to write function, but thank you for telling me that I am welcome to do so—that is very kind of you and, indeed, welcoming. I don't know whether to consider it an improvement. It is a nice function. ___ Python-ideas

[Python-ideas] Re: Delete dictionary entry if key exists using -= operator via __isub__()

2022-04-28 Thread Chris Angelico
On Fri, 29 Apr 2022 at 07:44, Steven D'Aprano wrote: > > Especially not when that solution is easily mistaken for something else: > > d -= 1 # Are we subtracting 1, or deleting key 1? > IMO it would make more sense to spell it as subtraction of two similar types: d -= {1} # supporting sets

[Python-ideas] Re: Delete dictionary entry if key exists using -= operator via __isub__()

2022-04-28 Thread Steven D'Aprano
On Thu, Apr 28, 2022 at 01:18:09AM -, zmvic...@gmail.com wrote: > *Background* > It is frequently desirable to delete a dictionary entry if the key > exists. Frequently? I don't think I've ever needed to do that. Can you give an example of real code that does this? > It is necessary to

[Python-ideas] Re: Delete dictionary entry if key exists using -= operator via __isub__()

2022-04-28 Thread Chris Angelico
On Fri, 29 Apr 2022 at 07:15, Zach Victor wrote: > > I agree that "implicit" does not mean "code that one dislikes." The intent is > "delete entry if key exists." Is that implicit or explicit? > "[R]emove specified key and return the corresponding value", with a default if there isn't one. Is

[Python-ideas] Re: Delete dictionary entry if key exists using -= operator via __isub__()

2022-04-28 Thread Zach Victor
I agree that "implicit" does not mean "code that one dislikes." The intent is "delete entry if key exists." Is that implicit or explicit? Positing a default value to discard it as a return value is, arguably, what makes the intent of that construction implicit. I say "arguably," because that

[Python-ideas] Re: Delete dictionary entry if key exists using -= operator via __isub__()

2022-04-28 Thread Chris Angelico
On Fri, 29 Apr 2022 at 04:03, Zach Victor wrote: > Where does this land with PEP 20? I think the use of pop() as you suggest > lands on the implicit side of things and is not as readable: the reader has > to ask, "what are we doing with the default value? Oh. Nothing. It's to > delete a dict

[Python-ideas] Re: Delete dictionary entry if key exists using -= operator via __isub__()

2022-04-28 Thread David Mertz, Ph.D.
Using `dct -= key` is WAAAYY too cryptic. A method would be okay, but since that is just `dct.pop(key, None)` without the second argument, it's not worth the trouble. -1 On Thu, Apr 28, 2022, 12:03 PM Zach Victor wrote: > Good points, Antoine! > > The intent is to mutate the dictionary with no

[Python-ideas] Re: Delete dictionary entry if key exists using -= operator via __isub__()

2022-04-28 Thread Zach Victor
Good points, Antoine! The intent is to mutate the dictionary with no side effects if a key exists —  and without the extra code that comes along with a control structure, a try except, or positing a None to throw away. For me, pop() says "give me something." Since the intent is to mutate the

[Python-ideas] Re: Delete dictionary entry if key exists using -= operator via __isub__()

2022-04-28 Thread Antoine Rozo
You could use the pop method on dictionary (with a default value of None for example) to remove the key only if it exists. The method returns the value associated with this key but you can ignore it. I'm not sure the __isub__ implementation would be easy to understand as it's weird to make a