> if Python had better function syntax and anonynous functions, decorators wouldn't even be a 'thing'
Better function syntax? What's wrong with python's function syntax and how does it relate to decorators?
Python does have anonymous functions, though they are limited: You can have only 1 expression, you can't have statements, and you can't have local variables. The reasoning is lambdas should be used for small operations; for everything else, named functions should be used.
The reasoning behind lambdas is arguable, but it still doesn't explain how it affects decorators.
Consider:
def memoize(fn):
from functools import wraps
@wraps(fn)
def memoized_fn(*args):
if not memoized_fn.cache.has_key(args):
memoized_fn.cache[args] = fn(*args)
return memoized_fn.cache[args]
memoized_fn.cache = {}
return memoized_fn
@memoize
def fib(n):
if n < 2: return n
return fib(n-1) + fib(n-2)
print fib(100)
What would a
better function syntax and
anonymous functions do so that
memoize decorator won't be a thing?