I have a python class that looks like this:
class Process:
def __init__(self, PID, PPID, cmd, FDs, reachable, user):
followed by:
self.PID=PID
self.PPID=PPID
self.cmd=cmd
...
Is there any way to autoinitialize these instance variables, like C++'s initialization list? It would spare lots of redundant code.
You could do it easily with the keyword arguments, e.g. like this:
similar implementation for the positional arguments would be:
which to me doesn't seem to solve your problem.
nu11ptr has made a small module, PyInstanceVars, which includes this functionality as a function decorator. In the module's README is states that the "[...] performance is now only 30-40% worse than explicit initialization under CPython".
Usage example, lifted straight from the module's documentation: