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
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:
>
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
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
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)
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
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
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
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:
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
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
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
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
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
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
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
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
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")
> ...
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,
>
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
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
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
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
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
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
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
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')
>
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
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 (
> 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
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
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
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
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
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
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
>
> ###
>
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
37 matches
Mail list logo