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

I'm inordinately fond of using decorators for adding metadata attributes as markup on functions:

  def add_example(*args):
    def decorate(fn):
      if not hasattr(fn, 'examples'):
        fn.examples = []
      fn.examples.insert(0, args)
      return fn
    return decorate
I use decorators like this to add extra command-line options to methods, annotate what types to construct from a command-line string, add typed __doc__ extensions, note example input/output for unit testing and documentation, etc.



Consider applying for YC's W25 batch! Applications are open till Nov 12.

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

Search: