I built a Polygon() class that originally only was initialized with veriticies:
But as the code evolved, I added a few @classmethods to construct the Polygon from other inputs. For example:
Now my problem is that I'm trying to find the canonical way to choose which method my polygon should be built with. I don't want users to have to use both constructors; rather, I want polygon to inspect its own keyword args and realize "hey, there's a center keyword, I'll construct from_center()" or "hey, verts were passed, I'll construct from verts" etc...
I thought that new would be the way to go; that is, new would do the inspection, and call the appropriate object. I'm not sure if this is correct, but I built a minimal example, but keep getting recursion erros. I can follow why, but don't know how to correct them. Any help would be tremendously appreciated.
class Foo(object): def __init__(self, *args, **kwargs): print 'initialized' % (args, kwargs) def __new__(cls, *args, **kwargs): print 'in new, deciding how to inialize' if 'a' in kwargs: return Foo.from_a(cls, *args, **kwargs) else: return Foo.not_from_a(cls, *args, **kwargs) @classmethod def from_a(cls, *args, **kwargs): print 'initializing "from_a()"' return Foo(*args, **kwargs) @classmethod def not_from_a(cls, *args, **kwargs): print 'initializing "not_from_a()"' return Foo(*args, **kwargs) if __name__ == '__main__': Foo(a=2)