Is there are good DRY fix for this painful design pattern?
Kirill Balunov
kirillbalunov at gmail.com
Tue Feb 27 10:45:43 EST 2018
Of course you can do the same without annotations, but with the
introduction of private attribute while your API it is under active
development:
from functools import wraps
def validate(func):
@wraps(func)
def _wrap(self, *args, **kwargs):
variables = self._vars # Here
kwargs.update(zip(variables, args))
for var in variables - kwargs.keys():
kwargs[var] = getattr(self, var)
return func(self, **kwargs)
return _wrap
class Foo:
def __init__(self, bashful, doc, dopey, grumpy,
happy, sleepy, sneezy):
self.bashful = bashful
self.doc = doc
self.dopey = dopey
self.grumpy = grumpy
self.happy = happy
self.sleepy = sleepy
self.sneezy = sneezy
self._vars = set(v for v in self.__dict__ if not
v.startswith('_')) # should be deleted when finish
@validate
def spam(self, bashful=None, doc=None, dopey=None,
grumpy=None, happy=None, sleepy=None,
sneezy=None):
return bashful, doc, dopey, grumpy, happy, sleepy, sneezy
With kind regards,
-gdg
More information about the Python-list
mailing list