2 ms·
I'm inordinately fond of using decorators for adding metadata attributes as markup on functions: def add_example(*args): def decorate(fn): if not h
by blasdel 17y ago
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.