Python decorator for automatic binding __init__ arguments
For future reference for all others who encounter this question in the future (Python 3.7 or above): There is the dataclass decorator that does exactly this.
Example
from dataclasses import dataclass
@dataclass
class Person:
name
age
address
# other methods...
It's definitely possible, here's a somewhat naive implementation:
from functools import wraps
def lazy_init(init):
import inspect
arg_names = inspect.getargspec(init)[0]
@wraps(init)
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
Once you start having something besides attributes that map to properties, you're going to run into trouble though. You'll need at least some way to specify which args to initialize as properties... at which point it'll just become more hassle than it's worth.