I've got fed up of continually typing the same, repetitive commands over and over again in my __init__
function. I was wondering if I could write a decorator to do the work for me. Here's an example of my question:
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
Is there some way in which I can automatically have all arguments passed into the function become instance variables with the same names? For example:
class Point:
@instance_variables
def __init__(self, x, y):
pass
Where @instance_variables
would automatically set self.x = x
and self.y = y
. How could I do this?
Thanks!
EDIT: I should mention that I use CPython 2.7.