Let's say you have a class that takes many (keyword) arguments, most of which are meant to be stored as instance variables:
class ManyInitVariables():
def __init__(a=0, b=2, c=1, d=0, e=-1, ... , x=100, y=0, z=9):
How would you initialize them in __init__
? You could do something like this:
class ManyInitVariables():
def __init__(a=0, b=2, c=1, d=0, e=-1, ... , x=100, y=0, z=9):
self.a = a
self.b = b
self.c = c
...
self.z = z
...but it would take a lot of typing! How could I get __init__
to automatically some of the arguments it takes, noting that other arguments may not need to be assigned as instance variables?
I'm sure there are many other similar solutions out there on the web for this very common issue, but this is one, for example:
Added: I've been asked to explain this further, but, there's a lot going on here if you're not skilled at Python!-).
functools.wraps
is a decorator to help make better decorators, see https://docs.python.org/2/library/functools.html#functools.wraps -- not directly germane to the question but useful to support interactivehelp
and tools based on functions' docstrings. Get into the habit of always using it when writing a function decorator that (the most common case) wraps the decorated function, and you won't regret it.The
inspect
module is the only right way to do introspection in modern Python.inspect.getargspec
in particular gives you information on what arguments a function accepts, and what the default values for them are, if any (the two bits of info I'm ignoring, by assigning them to_
, are about*a
and**k
special args, which this decorator doesn't support). See https://docs.python.org/2/library/inspect.html?highlight=getargspec#inspect.getargspec for more.self
, by convention, is always the first arg to a method (and this decorator is meant for methods only:-). So, the firstfor
loop deals with positional args (whether explicitly given in the call or defaulting to default values); then, the secondfor
loop deals with named args (that one, I hope, is simpler to grasp:-).setattr
of course is the precious built-in function which sets an attribute with a variable name, https://docs.python.org/2/library/functions.html?highlight=setattr#setattr for more.Incidentally, if you only care to use this in
__init__
(as you see in the example below,absorb_attrs
per se has no such constraint), then write a class decorator which singles out the class's__init__
for this treatment, and apply that class decorator to the class itself.Also, if your class's
__init__
has no work left to do once args are "absorbed" in this way, you must still define the (decorated)__init__
, but its body can be limited to a docstring explaining the arguments (I personally prefer to always also have apass
statement in such cases, but that's a personal style issue).And now, back to the original answer, with an example...!-)
And then, e.g, something like
would print
as desired.
My only excuse for "reinventing the wheel" this way (rather than scouring the web for a solution) is that the other evening my wife and co-author Anna (only ever woman winner of the Frank Willison Memorial Award for contribution to the Python community, BTW:-) asked me about it (we are, slowly alas!, writing the 3rd edition of "Python in a Nutshell") -- it took me 10 minutes to code this (sorry, no tests yet:-) while in the same 10 minutes she (despite being a very skilled web-searcher:-) could not locate an existing solution on the web. And, this way I need not worry about copyright issues if I want to post it here, include it in the next Nutshell, present about it at OSCON or Pycon, and so forth...:-)