Skip to content

Instantly share code, notes, and snippets.

@manuphatak
Created September 20, 2015 22:08
Show Gist options
  • Save manuphatak/e74f1f5197b8947af933 to your computer and use it in GitHub Desktop.
Save manuphatak/e74f1f5197b8947af933 to your computer and use it in GitHub Desktop.

Revisions

  1. manuphatak created this gist Sep 20, 2015.
    18 changes: 18 additions & 0 deletions default_args_class_decorator.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    from functools import wraps


    def defaults(method='__init__', **default_args):
    """Class decorator. Overrides method default arguments."""

    def decorate(cls):
    func = getattr(cls, method)

    @wraps(func)
    def wrapper(self, *args, **kwargs):
    default_args.update(kwargs)
    return func(self, *args, **default_args)

    setattr(cls, method, wrapper)
    return cls

    return decorate