I have created a class and I would like to give the user (probably me) two different ways to initialize it. For this example the class is called Box
and I would like to initialize it with either:
two sets of Cartesian coordinates, representing the min and max (x,y).
a set of Cartesian coordinates representing the min and the box dimensions
I can do this by using named parameters, however I would like to raise an error if no named parameter is called. My current code puts a dummy parameter in called pass_var
in the case that no named parameter is called. It works, but I think there must be a more elegant or Pythonic solution.
class Box(object):
def __init__(self, pass_var=None, coordinates=None, min_points=None, dimensions=None):
if coordinates:
setup_box_from_coordinates()
return
elif min_points and dimensions:
setup_box_from_dimensions()
return
raise NameError("Box Class needs to be initiated with 'coordinates='' or 'min_pts= & dimensions='"