Why not real anonymous functions?

My 2 cents. Maybe “tail defs” can be a good enough solution to cover common use cases?

So that this:

def deco(f):
  @wraps(f) 
  def wrapper(*args, **kwargs):
    print('calling f')
    return f(*args, **kwargs)
  return wrapper

def callback(framework_arg):
  pass

api_call(1, 2, callback, kw="example")

can become this:

def deco(f):
  return @wraps(f) def (*args, **kwargs):
    print('calling f')
    return f(*args, **kwargs)

api_call(1, 2, lambda, kw="example") def ():
  pass

So, basically, a single trailing anonymous def after a statement defines a function that can be referenced elsewhere in the statement.

Frankly, I don’t like how it looks, it’s quite limited and it barely saves space and keystrokes, but it helps to put things in more natural order, and conceptualy not far off from how decorators work today.

3 Likes