Using debug print routine inside assert

Alex Martelli aleax at aleax.it
Tue Nov 4 10:02:20 EST 2003


Richard Brodie wrote:
   ...
> def defer(f, *args):
>         f(*args)

Hmmm, this doesn't defer anything -- the moment you call defer, it in
turns calls f _at once_.

You can't impose lazy evaluation in Python unless the function you
are calling -- e.g. debug in the following:

>  debug("subfrobnicate(%s, %s) returned %s" % (p1, p2, defer(subfrobnicate,
>  p1, p2)))

_explicitly supports_ laziness -- e.g., accepts a callable and only
calls it if it needs its value (or, similarly, accepts an iterator and
only steps into it if it needs to, etc, etc).

So, for example, 'debug' would have to be changed to:

def debug(msg, *args):
    if not must_do_some_output: return

    if args:
        msg = msg % args[0](*args[1:])

    # proceed to output msg

and called as

debug("subfrobnicate(%s, %s) returned %%s" % (p1, p2), 
      subfrobnicate, p1, p2)


Alex





More information about the Python-list mailing list