Skip to content

Instantly share code, notes, and snippets.

@geovanisouza92
Created October 25, 2013 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geovanisouza92/7160767 to your computer and use it in GitHub Desktop.
Save geovanisouza92/7160767 to your computer and use it in GitHub Desktop.
Auto assign attributes on __init__
def lazy_init(init):
import inspect
arg_names = inspect.getargspec(init)[0]
def new_init(self, *args):
for name, value in zip(arg_names[1:], args):
setattr(self, name, value)
init(self, *args)
return new_init
class Person:
@lazy_init
def __init__(self, name, age):
pass
>>> p = Person("Derp", 13)
>>> print p.name, p.age
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment