python 2.7 - defining __call__ on a class as the constructor -
i'm trying use __call__
in way i'm not sure impacts.
i working base code data uses import return arbitrary item module this:
try: mod = __import__(mod_name, globals(), locals(), [object_name]) finally: sys.path.remove(search_folder) # object_name global variable in module return getattr(mod, object_name)
currently, each module define class , function. function in question create new instance of same class defined in module, passing given parameters class constructor.
to attempt avoid copy/paste function, changing call class, defined superclass call method, , inside method call same class constructor, this:
class foobar(object): def __init__(self, one, two): self.one = 1 self.two = 2 def __call__(cls, one, two): return cls(one, two)
it seems working on tests cases, i'm not sure impacts of using such design. should add have restriction of not changing current base code (i tried first @staticmethod
didn't work).
on other hand, don't have requirement define modules (class __call__
method), main objective apply dry. i'm not familiar python meta programming, maybe more elegant solution?
Comments
Post a Comment