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 OverflowCalling anonymous functions in Python without assigning them to a variable
Why not real anonymous functions? - Ideas - Discussions on Python.org
Why was the "lambda" keyword added for anonymous functions?
lambda - python: alternative to anonymous functions - Stack Overflow
Videos
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)
If your function is simple and only has expressions (no statements), then you can use lambda to create anonymous functions and call them inline.
>>> (lambda x, y: x*y)(3, 5)
15
I'm just thinking of other languages, i.e:
-
Rust:
|x| f(x) -
Scala
x => f(x)
Whereas Python has lambda x: f(x)
I know closures and anonymous functions are more ubiquitous in other languages but I still use them a fair amount in Python. I was just wondering why the choice made to put the lambda keyword in front.
Edit - I'm talking about the syntax choice from the language designers. I'm also aware you can do things like map(f, list).
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)
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.