Created
October 25, 2013 19:46
-
-
Save geovanisouza92/7160767 to your computer and use it in GitHub Desktop.
Auto assign attributes on __init__
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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