I have a method that draws a line or a patch of any shape I need given the name of the shape and some arguments, it works fine:
def draw_a_shape(ax, is_patch_not_line, shapename, **kwargs):
...
Now I need to have 30+ functions like draw_a_square_patch
, draw_an_ellipse_outline
etc. I can implement them in a trivial way, as below, but it's a bit painful:
def draw_a_square_patch(ax, **kwargs):
draw_a_shape(ax=ax, is_patch_not_line=True, shapename='a_square', **kwargs)
def draw_a_square_outline(ax, **kwargs):
draw_a_shape(ax=ax, is_patch_not_line=False, shapename='a_square', **kwargs)
def draw_an_ellipse_patch(ax, **kwargs):
draw_a_shape(ax=ax, is_patch_not_line=True, shapename='an_ellipse', **kwargs)
def draw_an_ellipse_outline(ax, **kwargs):
draw_a_shape(ax=ax, is_patch_not_line=False, shapename='an_ellipse', **kwargs)
.........
I wonder if there is a way to do it using a loop, and maybe something like setattr
? I am adding an invalid code below, just to explain what I mean:
for shapename in ['a_circle', 'a_square', ...]:
for is_patch_not_line in [False, True]:
new_func_name = "draw_" + shapename + "_"
if is_patch_not_line:
new_func_name += "patch"
else:
new_func_name += "outline"
global.setattr(new_func_name, "ax, **kwargs", ...)