[Python-ideas] a identity function?

Benjamin Peterson benjamin at python.org
Mon Mar 23 01:13:26 CET 2009

I've found as I write more and more decorators I need an identity function
often. For example I might write:

def replace_maybe(reason):
    if reason == "good reason":
        return lambda x: x
    def decorator(func):
        # do fancy stuff here
    return decorator

I hate lambdas, so usually I write

def _id(x):
    return x

It'd be nice to have a shortcut in the stdlib, though. Would this go well in the
operator or functools modules well?

More information about the Python-ideas mailing list