I want to write a function that creates a random graph. Since there are different types, I want to control its behaviour with a "mode" argument that supports multiple modes, e.g. "regular" and "er" for now. Each mode requires a different input parameters, and I'm interested in adding new modes that might also require a different number of input parameters. Is there a convention on how to implement this?
I can think of two options, first have a number of optional arguments that are only accessed when the appropriate mode is invoked:
def create_random_graph(number_of_vertices, mode = "regular", degree = 3, edge_probability = 0.5):
if mode == "regular":
#create random graph using only degree and never accessing edge_probability
elif mode == "er":
#create random graph using only edge_probability and never accessing degree
This is easy to invoke, but the function signature is ugly especially when adding more modes, and depending on the mode, some arguments aren't accessed at all.
Second option: The parameters need to be passed as a dictionary - harder to invoke and document and default values are somewhat obfuscated, but the signature doesn't blow up when adding more modes.
def create_random_graph(number_of_vertices, mode = "regular", param_dict = None):
if mode == "regular":
if param_dict is None:
degree = 3
else:
degree = param_dict["degree"]
#create the random degree-regular graph
#repeat for other modes accessing other dictionary items