[Python-Dev] lazy evaluation redux (was dict "setdefault". Feaure request or bugfix?)

Jp Calderone exarkun@intarweb.us
Tue, 11 Feb 2003 20:16:14 -0500


--9Ek0hoCL9XbhcSqy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Tue, Feb 11, 2003 at 04:30:48PM +0100, Jesus Cea Avion wrote:
> > One way to satisfy this would be to redefine setdefault something like
> > this:
> >=20
> > def setdefault(aDict, aKey, aValue=3DNone, lazy=3DNone):
> >    assert aValue is None or lazy is None
> >    if aKey not in aDict:
> >        aDict[aKey] =3D (if lazy is None: aValue else: lazy())
> >    return aDict[aKey]
>=20
> Nice. Nevertheless I keep thinking that current "setdefault" behaviour
> is not fully documented and counterintuitive :-). So we would need to
> fix the behaviour or update docs :-p

  To jump in, and re-iterate (for the third time?), setdefault is -not-
special.  It is a function call (a method call if you wish to be technical)
just like any other.  No function calls in Python behave the way you think
setdefault should, so the only way one can say that it would be more
intuitive the way you suggest is if one is basing one's intuition on other
languages wholely unrelated to Python.  If that is the case (What is the
currently popular expression?  Ah, yes...), perhaps you can make the change,
but you'll have to come up with a new name for the resulting language, for
it will not be Python.

  So, just to sum up:

  1) dict.setdefault is a regular method that is in no way special.

  2) No functions in Python have the "lazy evaluation" behavior you think
     dict.setdefault should have.

  3) No documentation describing that dict.setdefault does not lazily
     evaluate its second argument is missing, because no function in Python
     lazily evaluates its second argument.  (Perhaps some documentation cou=
ld
     be added a top-level some place, perhaps in the "Functions" section of
     the tutorial, pointing out that the second argument to a function is
     not lazily evaluated, though.)

  4) Creating a special behavior for a single function (see point #1) is not
     intuitive.  It is confusing and surprising.

   Jp

--=20
#!/bin/bash
( LIST=3D(~/.netscape/sigs/*.sig)
  cat ${LIST[$(($RANDOM % ${#LIST[*]}))]}
  echo --$'\n' `uptime` ) > ~/.netscape/.signature
--=20
 up 3 days, 6:28, 2 users, load average: 0.48, 0.33, 0.28

--9Ek0hoCL9XbhcSqy
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.0 (GNU/Linux)

iD8DBQE+SaBeedcO2BJA+4YRAhymAJkB1KfsMW09eo71/+E3/a9QvlHAewCeJpuw
8ELMRvhABZ5T09La4pli4R4=
=l/7i
-----END PGP SIGNATURE-----

--9Ek0hoCL9XbhcSqy--