varargs in lambda functions in Python

There is no problem using varargs in lambda functions. The issue here is different:

The problem is that the the lambda refrences the loop variable v. But by the time the lambda is called, the value of v has changed and the lambda calls the wrong function. This is always something to watch out for when you define a lambda in a loop.

You can fix this by creating an additional function which will hold the value of v in a closure:

def create_not_function(v):
    return lambda s, *args, **kw:  not v(s, *args, **kw)

for (k, v) in _dict.items():
    if hasattr(v, '__call__'):
        extended_dict["not_" + k] = create_not_function(v)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)