dict.get(key, default) evaluates default even if key exists
Loris Bennett
loris.bennett at fu-berlin.de
Wed Dec 16 04:54:39 EST 2020
Paul Bryan <pbryan at anode.ca> 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")
> ... return 1
> ...
>>>> get("a", generate_default())
> entering generate_default
> exiting generate_default
> entering get
> key='a' default=1
> exiting get
>>>>
OK, so it even has nothing to do with dict.get(). It is just about the
way function arguments are evaluated. Your example above illustrates
nicely that Python does what I would expect, although from the examples
others have given, I might have to adjust my expectations when using
other languages.
Cheers,
Loris
--
This signature is currently under construction.
More information about the Python-list
mailing list