dict.get(key, default): conditional default evaluation
Nikolas Kauer
nk at nkauer.com
Mon May 5 09:56:36 EDT 2003
I propose to not evaluate the expression default
in dict.get(key, default) if key in dict.
Example:
----------------------------------
>>> class A:
... def __init__(self):
... print "Constructing A instance"
...
>>> d = {1: 0}
>>> d.get(0, A())
Constructing A instance
<__main__.A instance at 0xa094bd8>
>>> d.get(1, A())
Constructing A instance
0
----------------------------------
I'd prefer if in the latter case A.__init__
would not be evaluated. Similar in spirit to
b in (a and b) not being evaluated if a is False.
More information about the Python-list
mailing list