Re: dict.get(key, default) evaluates default even if key exists

2020-12-20 Thread Chris Angelico
On Mon, Dec 21, 2020 at 12:47 AM <2qdxy4rzwzuui...@potatochowder.com> wrote: > > On 2020-12-19 at 22:29:34 +0100, > Roel Schroeven wrote: > > > What could be useful in some use cases, I think, is a wrapper function > > that evaluates the function lazily: > > > > def dict_get_lazily(d, key, fnc

Re: dict.get(key, default) evaluates default even if key exists

2020-12-20 Thread 2QdxY4RzWzUUiLuE
On 2020-12-19 at 22:29:34 +0100, Roel Schroeven wrote: > What could be useful in some use cases, I think, is a wrapper function > that evaluates the function lazily: > > def dict_get_lazily(d, key, fnc, *args, **kwargs): > try: > return d[key] > except KeyError: >

Re: dict.get(key, default) evaluates default even if key exists

2020-12-20 Thread Roel Schroeven
Ethan Furman schreef op 16/12/2020 om 17:59: Or, if the computationally massively expensive call uses potentially different arguments for each invocation: some_var = d.get('a') or cme(arg1, arg2, ...) I don't like that because it is very fragile: the expression will evaluate to the seco

RE: dict.get(key, default) evaluates default even if key exists

2020-12-18 Thread Schachner, Joseph
Yes. In order to call D.get( ) it needs to pass two arguments. The first is 'a', simple. The second is the result of a call to get_default(). So, that is called. From INSIDE get_default() it prints 'Nobody expects this' but you should expect it, get_default() gets executed. Following th

Re: dict.get(key, default) evaluates default even if key exists

2020-12-17 Thread jak
Il 17/12/2020 13:33, Chris Angelico ha scritto: On Thu, Dec 17, 2020 at 11:16 PM jak wrote: Il 17/12/2020 12:40, Peter J. Holzer ha scritto: On 2020-12-17 12:16:29 +0100, jak wrote: print(_ if d.get('a', None) is not None else get_default()) That doesn't work: print(_ if d.get('a', None)

Re: dict.get(key, default) evaluates default even if key exists

2020-12-17 Thread jak
Il 17/12/2020 12:40, Peter J. Holzer ha scritto: On 2020-12-17 12:16:29 +0100, jak wrote: print(_ if d.get('a', None) is not None else get_default()) That doesn't work: print(_ if d.get('a', None) is not None else get_default()) Traceback (most recent call last): File "", line 1, in Nam

Re: dict.get(key, default) evaluates default even if key exists

2020-12-17 Thread Chris Angelico
On Thu, Dec 17, 2020 at 11:16 PM jak wrote: > > Il 17/12/2020 12:40, Peter J. Holzer ha scritto: > > On 2020-12-17 12:16:29 +0100, jak wrote: > >> print(_ if d.get('a', None) is not None else get_default()) > > > > That doesn't work: > > > print(_ if d.get('a', None) is not None else get_defa

Re: dict.get(key, default) evaluates default even if key exists

2020-12-17 Thread jak
Il 17/12/2020 12:40, Peter J. Holzer ha scritto: On 2020-12-17 12:16:29 +0100, jak wrote: print(_ if d.get('a', None) is not None else get_default()) That doesn't work: print(_ if d.get('a', None) is not None else get_default()) Traceback (most recent call last): File "", line 1, in Nam

Re: dict.get(key, default) evaluates default even if key exists

2020-12-17 Thread Peter J. Holzer
On 2020-12-17 12:16:29 +0100, jak wrote: > print(_ if d.get('a', None) is not None else get_default()) That doesn't work: >>> print(_ if d.get('a', None) is not None else get_default()) Traceback (most recent call last): File "", line 1, in NameError: name '_' is not defined But this works:

Re: dict.get(key, default) evaluates default even if key exists

2020-12-17 Thread jak
Il 17/12/2020 12:16, jak ha scritto: Il 15/12/2020 18:07, Mark Polesky ha scritto: Hi. # Running this script D = {'a':1} def get_default(): print('Nobody expects this') return 0 print(D.get('a', get_default())) # ...generates this output: Nobody expects this 1 ### Since I'm b

Re: dict.get(key, default) evaluates default even if key exists

2020-12-17 Thread jak
Il 15/12/2020 18:07, Mark Polesky ha scritto: Hi. # Running this script D = {'a':1} def get_default():     print('Nobody expects this')     return 0 print(D.get('a', get_default())) # ...generates this output: Nobody expects this 1 ### Since I'm brand new to this community, I thought

Re: dict.get(key, default) evaluates default even if key exists

2020-12-17 Thread Greg Ewing
On 17/12/20 8:31 am, Grant Edwards wrote: On 2020-12-16, Dennis Lee Bieber wrote: https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_name I vaguely recall some other old language I ran across in a survey course when in college that used it. IIRC it was Algol. Algol 60. Also, Lisp ma

Re: dict.get(key, default) evaluates default even if key exists

2020-12-16 Thread Grant Edwards
On 2020-12-16, Dennis Lee Bieber wrote: > On Tue, 15 Dec 2020 20:08:53 + (UTC), Mark Polesky via Python-list > declaimed the following: > >>behavior, and I can't remember any programming language in which it's >>different. > > https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_name Mo

Re: dict.get(key, default) evaluates default even if key exists

2020-12-16 Thread Ethan Furman
On 12/16/20 3:08 AM, Peter J. Holzer wrote: On 2020-12-15 13:07:25 -0800, Ethan Furman wrote: On 12/15/20 9:07 AM, Mark Polesky via Python-list wrote: D = {'a':1} def get_default(): print('Nobody expects this') return 0 print(D.get('a', get_default())) Python has short-circuiti

Re: dict.get(key, default) evaluates default even if key exists

2020-12-16 Thread Ethan Furman
On 12/16/20 1:44 AM, Chris Angelico wrote: On Wed, Dec 16, 2020 at 8:43 PM Loris Bennett wrote: Paul Bryan writes: On Wed, 2020-12-16 at 10:01 +0100, Loris Bennett wrote: OK, I get the point about when the default value is generated and that potentially being surprising, but in the exampl

Re: dict.get(key, default) evaluates default even if key exists

2020-12-16 Thread Chris Angelico
On Wed, Dec 16, 2020 at 10:17 PM Peter J. Holzer wrote: > > On 2020-12-15 13:07:25 -0800, Ethan Furman wrote: > > On 12/15/20 9:07 AM, Mark Polesky via Python-list wrote: > > > > > D = {'a':1} > > > > > > def get_default(): > > > print('Nobody expects this') > > > return 0 > > > > > > pr

Re: dict.get(key, default) evaluates default even if key exists

2020-12-16 Thread Peter J. Holzer
On 2020-12-15 13:07:25 -0800, Ethan Furman wrote: > On 12/15/20 9:07 AM, Mark Polesky via Python-list wrote: > > > D = {'a':1} > > > > def get_default(): > > print('Nobody expects this') > > return 0 > > > > print(D.get('a', get_default())) > > Python has short-circuiting logical operat

Re: dict.get(key, default) evaluates default even if key exists

2020-12-16 Thread Loris Bennett
Paul Bryan writes: > Maybe this will help: > def get(key, default): > ... print("entering get") > ... print(f"{key=} {default=}") > ... print("exiting get") > ... def generate_default(): > ... print("entering generate_default") > ... print("exiting generate_default") > ...

Re: dict.get(key, default) evaluates default even if key exists

2020-12-16 Thread Chris Angelico
On Wed, Dec 16, 2020 at 8:43 PM Loris Bennett wrote: > > Paul Bryan writes: > > > On Wed, 2020-12-16 at 10:01 +0100, Loris Bennett wrote: > > > >> OK, I get the point about when the default value is generated and > >> that > >> potentially being surprising, but in the example originally given, >

Re: dict.get(key, default) evaluates default even if key exists

2020-12-16 Thread Loris Bennett
Paul Bryan writes: > On Wed, 2020-12-16 at 10:01 +0100, Loris Bennett wrote: > >> OK, I get the point about when the default value is generated and >> that >> potentially being surprising, but in the example originally given, >> the >> key 'a' exists and has a value of '1', so the default value i

Re: dict.get(key, default) evaluates default even if key exists

2020-12-16 Thread Paul Bryan
Maybe this will help: >>> def get(key, default): ... print("entering get") ... print(f"{key=} {default=}") ... print("exiting get") ... >>> def generate_default(): ... print("entering generate_default") ... print("exiting generate_default") ... return 1 ... >>> get("a", generate_defa

Re: dict.get(key, default) evaluates default even if key exists

2020-12-16 Thread Paul Bryan
On Wed, 2020-12-16 at 10:01 +0100, Loris Bennett wrote: > OK, I get the point about when the default value is generated and > that > potentially being surprising, but in the example originally given, > the > key 'a' exists and has a value of '1', so the default value is not > needed. But the func

Re: dict.get(key, default) evaluates default even if key exists

2020-12-16 Thread Loris Bennett
Paul Bryan writes: > On Wed, 2020-12-16 at 08:59 +0100, Loris Bennett wrote: > >> Isn't the second argument to D.get() the value to be return if the >> first >> argument is not a valid key?  In that case, why does it make any >> difference here what the second argument of D.get() is since the key

Re: dict.get(key, default) evaluates default even if key exists

2020-12-16 Thread Paul Bryan
On Wed, 2020-12-16 at 08:59 +0100, Loris Bennett wrote: > Isn't the second argument to D.get() the value to be return if the > first > argument is not a valid key?  In that case, why does it make any > difference here what the second argument of D.get() is since the key > 'a' > does exist? > > Th

Re: dict.get(key, default) evaluates default even if key exists

2020-12-16 Thread Loris Bennett
Serhiy Storchaka writes: > 15.12.20 19:07, Mark Polesky via Python-list пише: >> # Running this script >> >> D = {'a':1} >> def get_default(): >>     print('Nobody expects this') >>     return 0 >> print(D.get('a', get_default())) >> >> # ...generates this output: >> >> Nobody expects this

Re: dict.get(key, default) evaluates default even if key exists

2020-12-15 Thread Matt Ruffalo
On 15/12/20 15:26, Grant Edwards wrote: > On 2020-12-15, Mark Polesky via Python-list wrote: > >> I see. Perhaps counterintuitive, > I guess that depends on what programming language you normally think > in. Python's handling of function parameters is exactly what I > expected, because all of the

Re: dict.get(key, default) evaluates default even if key exists

2020-12-15 Thread 2QdxY4RzWzUUiLuE
On 2020-12-16 at 12:01:01 +1300, dn via Python-list wrote: > > On Tue, Dec 15, 2020 at 9:57 AM Mark Polesky via Python-list < > > python-list@python.org> wrote: > > > >> Hi. > >> > >> # Running this script > >> > >> D = {'a':1} > >> def get_default(): > >> print('Nobody expects this') >

Re: dict.get(key, default) evaluates default even if key exists

2020-12-15 Thread Chris Angelico
On Wed, Dec 16, 2020 at 10:03 AM dn via Python-list wrote: > > On 16/12/2020 07:52, Dan Stromberg wrote: > ...> BTW, I tend to prefer collections.defaultdict over the two argument > D.get > > or setdefault. > > > Contrarily, dict.get() seems 'better', unless (a) the dict's values are > all to be i

Re: dict.get(key, default) evaluates default even if key exists

2020-12-15 Thread dn via Python-list
On 16/12/2020 07:52, Dan Stromberg wrote: ...> BTW, I tend to prefer collections.defaultdict over the two argument D.get or setdefault. Contrarily, dict.get() seems 'better', unless (a) the dict's values are all to be initialised to the same value, eg all None, int 0, or empty list []; or (

Re: dict.get(key, default) evaluates default even if key exists

2020-12-15 Thread dn via Python-list
> On Tue, Dec 15, 2020 at 9:57 AM Mark Polesky via Python-list < > python-list@python.org> wrote: > >> Hi. >> >> # Running this script >> >> D = {'a':1} >> def get_default(): >> print('Nobody expects this') >> return 0 >> print(D.get('a', get_default())) >> >> # ...generates this out

Re: dict.get(key, default) evaluates default even if key exists

2020-12-15 Thread Dan Stromberg
On Tue, Dec 15, 2020 at 11:05 AM Serhiy Storchaka wrote: > 15.12.20 19:07, Mark Polesky via Python-list пише: > > # Running this script > > > > D = {'a':1} > > def get_default(): > > print('Nobody expects this') > > return 0 > > print(D.get('a', get_default())) > > > > # ...generates

Re: dict.get(key, default) evaluates default even if key exists

2020-12-15 Thread Ethan Furman
On 12/15/20 9:07 AM, Mark Polesky via Python-list wrote: > D = {'a':1} > > def get_default(): > print('Nobody expects this') > return 0 > > print(D.get('a', get_default())) Python has short-circuiting logical operations, so one way to get the behavior you're looking for is: D.get

Re: dict.get(key, default) evaluates default even if key exists

2020-12-15 Thread Grant Edwards
On 2020-12-15, Mark Polesky via Python-list wrote: > I see. Perhaps counterintuitive, I guess that depends on what programming language you normally think in. Python's handling of function parameters is exactly what I expected, because all of the previous languages I used did the same thing. Pu

Re: dict.get(key, default) evaluates default even if key exists

2020-12-15 Thread Mark Polesky via Python-list
I see. Perhaps counterintuitive, but implemented consistently. Add it to the list of gotchas, I guess. By the way... four helpful responses in under an hour, very impressive. Nice community here. Thanks to all who answered. Mark On Tuesday, December 15, 2020, 11:05:10 AM PST, Serhiy Storc

Re: dict.get(key, default) evaluates default even if key exists

2020-12-15 Thread Serhiy Storchaka
15.12.20 19:07, Mark Polesky via Python-list пише: > # Running this script > > D = {'a':1} > def get_default(): >     print('Nobody expects this') >     return 0 > print(D.get('a', get_default())) > > # ...generates this output: > > Nobody expects this > 1 > > ### > > Since I'm brand new t

Re: dict.get(key, default) evaluates default even if key exists

2020-12-15 Thread Chris Angelico
On Wed, Dec 16, 2020 at 4:58 AM Mark Polesky via Python-list wrote: > > Hi. > > # Running this script > > D = {'a':1} > def get_default(): > print('Nobody expects this') > return 0 > print(D.get('a', get_default())) > > # ...generates this output: > > Nobody expects this > 1 > > ### >

Re: dict.get(key, default) evaluates default even if key exists

2020-12-15 Thread Dan Stromberg
On Tue, Dec 15, 2020 at 9:57 AM Mark Polesky via Python-list < python-list@python.org> wrote: > Hi. > > # Running this script > > D = {'a':1} > def get_default(): > print('Nobody expects this') > return 0 > print(D.get('a', get_default())) > > # ...generates this output: > > Nobody exp