Hacker News new | past | comments | ask | show | jobs | submit login

Useful example, adding memoization without rewriting the actual function, just add @decoratorname above the function.

class memoize: def __init__(self, function): self.function = function self.memoized = {}

    def __call__(self, *args):
        try:
            return self.memoized[args]
        except KeyError:
            self.memoized[args] = self.function(*args)
            return self.memoized[args]


  @memoize
  def fib(n)



2.5 supported function decorators, which this is. (The decorator has always been a callable, so the above class has always been legal.)

2.6 supports decorators for other things, including classes.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: