Where is quote (again)?
Russell E. Owen
owen at xastrox.xwashingtonx.edu.invalid
Fri Mar 8 16:21:23 EST 2002
In article <1f5252d4.0203080721.3bfc5c14 at posting.google.com>,
nbecker at fred.net (N Becker) wrote:
>Back in 1999, I asked "where is quote?". I was looking for
>a way to quote a function (delay evaluation).
>
>I wonder if there is anything new. It looks to me that the
>best answer is to use lambda.
There are two aspects to this sort of function, data you want to set up
in advance and possibly data you want the function to see later but that
you don't want to pass as an argument.
For the latter, you can take advantage of Python's new scoping rules.
They have some quirks, but will probably do the job.
For setting up parameters in advance, there are several ways. My
favorite is to use a callable class, and here is a lovely general one
posted by Eric Brunel (if Eric is reading this: did you write it, if
not, do you know who did?):
class GenericCallback:
def __init__(self, callback, *firstArgs):
self.__callback = callback
self.__firstArgs = firstArgs
def __call__(self, *lastArgs):
apply(self.__callback, self.__firstArgs + lastArgs)
Certainly you can use lambdas, specifying default arguments for data you
are evaluating at function creation time. Personally, I dislike lambdas;
defining a named function works just as well, is usually easier to read,
and allows more flexibility about what the function can do. Still, in
either case those default arguments are somewhat ugly. It's hard to beat
GenericCallback for clarity and simplicity.
-- Russell
--
Return owen
address astro
garbled washington
in header edu
More information about the Python-list
mailing list