The code creates a meta class and its implementation to set the values passed in as arguments to methods on a class automatically on the object avoiding a lot of self.a=a code at the start of methods.
>>> from functools import wraps
>>> import inspect
>>>
>>> def setMethodAttrs(func):
... @wraps(func)
... def wrapper(*args, **kwargs):
... formalArgs=inspect.getargspec(func)[0]
... if len(args)>1:
... for x in range(1, len(args)):
... setattr(args[0], formalArgs[x], args[x])
... for k, v in kwargs.iteritems():
... setattr(vars()['args'][0], k, v)
... return func(*args, **kwargs)
... return wrapper
...
>>> def classAttrs(cls):
... for key, val in vars(cls).items():
... if callable(val):
... setattr(cls,key, setMethodAttrs(val))
... return cls
...
...
>>>
>>> @classAttrs
... class trythis:
... def __init__(self):
... self.firstvar=1
... self.secondvar=2
... self.thirdvar=3
... self.fourthvar=4
... def method1(self,firstvar=None,secondvar=None,thirdvar=None):
... print("firstvar {}".format(self.firstvar))
... print("secondvar {}".format(self.secondvar))
... print("thirdvar {}".format(self.thirdvar))
... print("fourthvar {}".format(self.fourthvar))
... def method2(self, secondvar=None, fourthvar=None):
... print("firstvar {}".format(self.firstvar))
... print("secondvar {}".format(self.secondvar))
... print("thirdvar {}".format(self.thirdvar))
... print("fourthvar {}".format(self.fourthvar))
...
...
...
>>> a=trythis()
>>> a.method1()
firstvar 1
secondvar 2
thirdvar 3
fourthvar 4
>>> a.method1(firstvar=9)
firstvar 9
secondvar 2
thirdvar 3
fourthvar 4
>>> a.method2(11)
firstvar 9
secondvar 11
thirdvar 3
fourthvar 4
>>> a.method1(19, thirdvar=21)
firstvar 19
secondvar 11
thirdvar 21
fourthvar 4
>>> a.method2(fourthvar=40)
firstvar 19
secondvar 11
thirdvar 21
fourthvar 40
>>>