Skip to content

Instantly share code, notes, and snippets.

View marcocamma's full-sized avatar

marco cammarata marcocamma

View GitHub Profile
@marcocamma
marcocamma / auto_args.py
Last active April 30, 2017 00:14 — forked from judy2k/auto_args.py
Save constructor arguments on self without writing self.x = x etc...
from inspect import signature
def auto_args(f):
sig = signature(f) # Get a signature object for the target:
def replacement(self, *args, **kwargs):
# Parse the provided arguments using the target's signature:
bound_args = sig.bind(self, *args, **kwargs)
# add defaults otherwise defaults are unbonund, thus not in the list
bound_args.apply_defaults();
# Save away the arguments on `self`: