3 ms·
Didn't downvote you, but the difference is that lambdas can only be executed (i.e. applied to something) while decorators let you analyze the code of the functi
by miloshasan 16y ago
Didn't downvote you, but the difference is that lambdas can only be executed (i.e. applied to something) while decorators let you analyze the code of the function and do something else other than just executing it.
- ekiru 16y agoHow is this so? Decorators are just syntactic sugar for defining a function then replacing it with the result of calling another function (the decorator) on it.
- miloshasan 16y agoThe decorator can do introspection on the function object. This project uses the idea to execute (some) Python code on the GPU: http://code.google.com/p/copperhead/ http://code.google.com/p/copperhead/
- JeremyBanks 16y agoWhich being able to wrap the function definition in a function call could accomplish. Unfortunately that's not the direction the Python team wants to take the language.
- cabalamat 16y ago> The decorator can do introspection on the function object. so can normal (i.e. not using decorators) code after the the function, e.g.: def myFunction(x): blah doSomethingWith(myFunction) Here, doSomethingWith() can introspect on myFunction()
- miloshasan 16y agoAh, yes - of course decorators do not fundamentally increase the expressive power of the language (and neither do lambdas, obviously). It's all just a matter of convenience and elegance, which are in the eye of the beholder anyway...