Decorators, Shmecorators

This is how I learned to write decorators:

class Decorator:
  def __init__(self, func):
    self.func = func

  def __call__(self, *args):
    # do stuff
    return self.func(*args)

But today I was reminded of the classic way of writing decorators:

def mydecorator(func):
  def mydecorator_guts(*args, **kwargs):
    # do stuff
    return func(*args, **kwargs)
  return mydecorator_guts

Simpler and more to the point. And NO CLASSES. This ain’t Java 🙂

This entry was posted in Coding.