Edit: Adam asked me to extend the solution to support keyword arguments
Here is the final solution:
from functools import wraps
import inspect
def initializer(fun):
names, varargs, keywords, defaults = inspect.getargspec(fun)
@wraps(fun)
def wrapper(self, *args, **kargs):
for name, arg in zip(names[1:], args) + kargs.items():
setattr(self, name, arg)
fun(self, *args, **kargs)
return wrapper
You can use a decorator:
from functools import wraps
import inspect
def initializer(fun):
names, varargs, keywords, defaults = inspect.getargspec(fun)
@wraps(fun)
def wrapper(self, *args):
for name, arg in zip(names[1:], args):
setattr(self, name, arg)
fun(self, *args)
return wrapper
class process:
@initializer
def __init__(self, PID, PPID, cmd, FDs, reachable, user):
pass
Output:
>>> c = process(1, 2, 3, 4, 5, 6)
>>> c.PID
1
>>> dir(c)
['FDs', 'PID', 'PPID', '__doc__', '__init__', '__module__', 'cmd', 'reachable', 'user'