Getting a set of lambda functions

Terry Reedy tjreedy at udel.edu
Sun May 25 20:46:48 EDT 2008


"I V" <ivlenin at gmail.com> wrote in message 
news:eci_j.4164$mh5.917 at nlpi067.nbdc.sbc.com...
| On Sun, 25 May 2008 13:43:15 +0200, Martin Manns wrote:
| > I try to get a set of lambda functions that allows me executing each

I think it worth the reminder that Python has lambda *expressions* that 
result in function objects that identical to the function objects produced 
by def statements (except for the special name '<lambda>').   For the 
purpose of this thread,comparing functions by comparing their code objects, 
the name attribute is irrelevant.  So this discussion has nothing to do 
with the syntactic form of function definitions.

| > function code exactly once. Therefore, I would like to modify the set
| > function to compare the func_code properties (or the lambda functions 
to
| > use this property for comparison).
|
| With Ivan's approach, you lose access to the actual lambdas,

I presume you mean function object rather than lambda expression

| so you need
| to create a new function and then modify its code object to actually call
| the code; this seems a little clunky to me. You might instead want to
| wrap the lambdas in an object that will do the comparison you want:
|
| class Code(object):
| def __init__(self, func):
| self._func = func
|
| def __cmp__(self, other):
| return cmp(self._func.func_code, other._func.func_code)
|
| def __call__(self, *args, **kwargs):
| return self._func(*args, **kwargs)

If one could subclass from function instead of object, this would be even 
simpler (even with Bearophile's addition) since no __call__ would be 
needed.  But we cannot, partly because there did not seem to be much use 
case when this was once discussed.   But here is one ;-)

tjr






More information about the Python-list mailing list