Getting a set of lambda functions

I V ivlenin at gmail.com
Sun May 25 14:27:22 EDT 2008


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
> 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, 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)

func_set = set(Code(f) for f in funclist)



More information about the Python-list mailing list