Welcome, guest | Sign In | My Account | Store | Cart

A very simple recipe to allow you easily name lambda-objects (or other kind of objects, callable, for example, partial objects) you create, with little overhead and friendly syntax.

Python, 12 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
def Name(**kw):
    assert len(kw)==1
    name, obj = kw.items()[0]
    obj.func_name = name
    return obj

def main():
    f = Name(CheckExists=lambda:os.path.exists(some_filename))
    print f

if __name__ == '__main__':
    main()

Whenever you create a lambda, it have no "function name" assigned as a regular created function would have. Indeed it will have "<lambda>" as its name. Also, we you create a lambda you are expecting to do it simply in a few lines. In this way I've created this very little recipe in order to avoid this problem.

( https://gist.github.com/tenuki/f5eb5ea3274c58db3395#file-gistfile1-py )