[Python-Dev] Re: Dict __missing__ consistency issues.

2020-01-08 Thread Bar Harel
Finished creating 2 issues on the meanwhile: bpo-39264 to fix UserDict.get(). bpo-39267 to fix dict.__missing__ & dict.get() documentation. Both include a PR. ___ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-

[Python-Dev] Re: Dict __missing__ consistency issues.

2020-01-08 Thread Bar Harel
> > [Bar Harel] > > As for get(). Current implementation of UserDict returns the default > value > > for get() if __missing__() raises KeyError. Which sounds reasonable to > me. > > After all, you would logically expect __missing__ to be called for get() > > as you tried to get a non-existing valu

[Python-Dev] Re: Dict __missing__ consistency issues.

2020-01-08 Thread Guido van Rossum
On Wed, Jan 8, 2020 at 12:02 PM Ethan Furman wrote: > I disagree. My understanding of the purpose behind get() is to get a value > or return the default specified in the get() call, not to create a new key > (which the default __missing__ does). > Right. For comparison, defaultdict.get() doesn'

[Python-Dev] Re: Dict __missing__ consistency issues.

2020-01-08 Thread Ethan Furman
[Bar Harel]: I was just about to fix dict's get() to call __missing__ if a key doesn't exist (before returning the default value) but realized that although small, that patch can cause future issues. Right now there's an inconsistency: The reason for this inconsistency is because the Mapping ab

[Python-Dev] Re: Dict __missing__ consistency issues.

2020-01-08 Thread Guido van Rossum
Sounds good to me. If you want a second opinion als Raymond Hettinger. On Wed, Jan 8, 2020 at 11:09 Bar Harel wrote: > Well it depends on the consensus for __missing__, and the consensus for > get(). > > I believe modifying get() is out of the question as it will lead to > breakage. If so, whet

[Python-Dev] Re: Dict __missing__ consistency issues.

2020-01-08 Thread Bar Harel
Well it depends on the consensus for __missing__, and the consensus for get(). I believe modifying get() is out of the question as it will lead to breakage. If so, whether it's a quirk or not doesn't matter as we can't change it. Adding a comment to the docs should suffice, along with updating the

[Python-Dev] Re: Dict __missing__ consistency issues.

2020-01-08 Thread Guido van Rossum
Hum, it looks like the UserDict.get() behavior is an accident due to the way Mapping.get() is implemented. The comment there says 'D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.' but the implementation just tries D[k] and catches KeyError. The UserDict.__contains__() implementat

[Python-Dev] Re: Dict __missing__ consistency issues.

2020-01-08 Thread Bar Harel
Sorry for the double post then :-) As for get(). Current implementation of UserDict returns the default value for get() if __missing__() raises KeyError. Which sounds reasonable to me. After all, you would logically expect __missing__ to be called for get() as you tried to get a non-existing value

[Python-Dev] Re: Dict __missing__ consistency issues.

2020-01-08 Thread Guido van Rossum
(Note: We received your email twice.) I don't think __missing__ should be called by get() -- get() already has a way to deal with missing keys, and making it use two different mechanisms would be weird (e.g. if get() calls __missing__, is the default value ever used?). To answer your second quest