I don't think is possible, given your comments to the lambda answers. The lambda operator is Python's only way of supporting anonymous functions. If you need to support statements, then you need to use a def, which always must be named.

Keep in mind that lambdas can support limited if-then logic, however. The following two are lambda expressions that implements the if-then logic given above:

(lambda a: alert(1 if a == 1 else 0))(1)

More literally:

(lambda a: alert(a) if a == 1 else alert(0))(1)
Answer from HardlyKnowEm on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-lambda-anonymous-functions-filter-map-reduce
Python Lambda Functions - GeeksforGeeks
Lambda keyword (lambda): Defines an anonymous (inline) function in Python.
Published   3 weeks ago
Discussions

Calling anonymous functions in Python without assigning them to a variable
If your function is simple and only has expressions (no statements), then you can use lambda to create anonymous functions and call them inline. ... I know about lambda's. However, I am interested in the use of statements within functions as well 2012-02-18T15:57:38.54Z+00:00 ... @Opsa: Python does ... More on stackoverflow.com
🌐 stackoverflow.com
Why not real anonymous functions? - Ideas - Discussions on Python.org
For whatever reason I often find myself writing code like this: def returns_a_func(this_var_gets_used): def _inner_func(*args, **kwargs): """Do some stuff in here""" return _inner_func Or like this: def some_useful_func(*args, **kwargs): """Does useful and interesting things, I promise""" ... More on discuss.python.org
🌐 discuss.python.org
4
January 30, 2024
Why was the "lambda" keyword added for anonymous functions?
Is your question why it's "lambda" in particular? It's because anonymous functions like that are very much inspired by the lambda calculus where you'd write λx.f x for python's lambda x: f(x) and it's just a "pythonization" of that syntax. If you move more into the direction of functional languages you'll find this notation more often: for example lean has λ x, f x instead (as well as I think x => f x in lean 4?) Haskell also has a similar notation - IIRC it's \x -> f x (where the \ is supposed to look kind of like a lambda) or alternatively λx -> f x (I'm not 100% sure on that second one but I think it's valid) More on reddit.com
🌐 r/Python
110
272
November 3, 2023
lambda - python: alternative to anonymous functions - Stack Overflow
Python doesn't support complicated anonymous functions. What's a good alternative? For example: class Calculation: def __init__(self, func): self.func = func def __call__(self, d... More on stackoverflow.com
🌐 stackoverflow.com
🌐
W3Schools
w3schools.com › python › python_lambda.asp
Python Lambda
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... A lambda function is ...
🌐
Medium
saketraj200.medium.com › python-anonymous-lambda-function-4dd20bcc99c
Python Anonymous/Lambda Function. An anonymous function is a function… | by saket raj | Medium
April 11, 2021 - An anonymous function is a function that is defined without a name. While normal functions are defined using the def keyword in Python, anonymous functions are defined using the lambda keyword.
🌐
Programiz
programiz.com › python-programming › anonymous-function
Python Lambda/ Function (With Examples)
In Python, a lambda function is a special type of function without the function name.
🌐
Python.org
discuss.python.org › ideas
Why not real anonymous functions? - Page 2 - Ideas - Discussions on Python.org
January 30, 2024 - For whatever reason I often find myself writing code like this: def returns_a_func(this_var_gets_used): def _inner_func(*args, **kwargs): """Do some stuff in here""" return _inner_func Or like this: d…
Find elsewhere
🌐
Tutorial Teacher
tutorialsteacher.com › python › python-lambda-function
Lambda Functions and Anonymous Functions in Python
Above, lambda x: x*x defines an anonymous function and call it once by passing arguments in the parenthesis (lambda x: x*x)(5). In Python, functions are the first-class citizens, which means that just as literals, functions can also be passed as arguments.
🌐
Real Python
realpython.com › python-lambda
How to Use Python Lambda Functions – Real Python
December 1, 2023 - In Python, an anonymous function is created with the lambda keyword. More loosely, it may or not be assigned a name. Consider a two-argument anonymous function defined with lambda but not bound to a variable.
🌐
Python.org
discuss.python.org › ideas
Why not real anonymous functions? - Ideas - Discussions on Python.org
January 30, 2024 - For whatever reason I often find myself writing code like this: def returns_a_func(this_var_gets_used): def _inner_func(*args, **kwargs): """Do some stuff in here""" return _inner_func Or like this: d…
🌐
Wikipedia
en.wikipedia.org › wiki › Anonymous_function
Anonymous function - Wikipedia
February 18, 2026 - Anonymous functions have been a feature of programming languages since Lisp in 1958, and a growing number of modern programming languages support anonymous functions. The names "lambda abstraction", "lambda function", and "lambda expression" refer to the notation of function abstraction in lambda calculus, where the usual function f(x) = M would be written (λx.M), and where M is an expression that uses x. Compare to the Python syntax of lambda x: M.
🌐
LabEx
labex.io › tutorials › python-how-to-use-anonymous-functions-in-python-398259
How to use anonymous functions in Python | LabEx
Python's anonymous functions, also known as lambda functions, offer a concise and flexible way to write small, one-time functions without the need for a formal function definition.
🌐
Codecademy
codecademy.com › docs › python › functions › anonymous functions
Python | Functions | Anonymous Functions | Codecademy
April 23, 2022 - Such functions are called “anonymous” and are defined using the lambda keyword. ... The following two definitions are equivalent. ... The expression to the right of the assignment operator is called a “lambda expression”. The Python ...
🌐
BelieveMy
believemy.com › en › glossaries › python › lambda
Anonymous functions with Python (lambda) | Python glossary
In Python, the lambda keyword is used to define anonymous (i.e. unnamed) functions. We call them lambda functions.
Top answer
1 of 3
5

You don't need anonymous functions. Also, memoization has been done better than this, there's probably no reason for you to roll your own.

But to answer the question: You can use your class as a decorator.

@Calculation
def f2():
    ...

This simply defined the function, wraps it in Calculation and stored the result of that as f2. The decorator syntax is defined to be equivalent to:

_decorator = Calculation # a fresh identifier
# not needed here, but in other cases (think properties) it's useful
def f2():
    ...
f2 = _decorator(f2)
2 of 3
4

The alternative to an anonymous function is a non-anonymous function. An anonymous function is only anonymous in the context where it was defined. But it is not truly anonymous, because then you could not use it.

In Python you make anonymous functions with the lambda statement. You can for example do this:

output = mysort(input, lambda x: x.lastname)

The lambda will create a function, but that function has no name in the local space, and it's own name for itself is just '<lambda>'. But if we look at mysort, it would have to be defined something like this:

def mysort(input, getterfunc):
    blahblahblah

As we see here, in this context the function isn't anonymous at all. It has a name, getterfunc. From the viewpoint of this function it does not matter if the function passed in are anonymous or not. This works just as well, and is exactly equivalent in all significant ways:

def get_lastname(x):
    return x.lastname

output = mysort(input, get_lastname)

Sure, it uses more code, but it is not slower or anything like that. In Python, therefore anonymous functions are nothing but syntactic sugar for ordinary functions.

A truly anonymous function would be

lambda x: x.lastname

But as we don't assign the resulting function to anything, we do not get a name for the function, and then we can't use it. All truly anonymous functions are unusable.

For that reason, if you need a function that can't be a lambda, make it an ordinary function. It can never be anonymous in any meaningful way, so why bother making it anonymous at all? Lambdas are useful when you want a small one-line function and you don't want to waste space by defining a full function. That they are anonymous are irrelevant.

🌐
freeCodeCamp
freecodecamp.org › news › python-lambda-functions
Python Lambda Functions – How to Use Anonymous Functions with Examples
February 24, 2023 - Lambda functions, also known as anonymous functions, are small, one-time-use functions in Python. You can define them using the lambda keyword followed by the function's inputs, a colon, and the function's expression.
🌐
AskPython
askpython.com › home › python lambda – anonymous function
Python lambda - Anonymous Function - AskPython
September 5, 2019 - Python lambda functions are also called an anonymous function. They don't have any name. Python lambda function examples with map(), filter(), and reduce().