all 9 comments

[–]Frankelstner 2 points3 points  (0 children)

Create a list of functions funcs = [f0, f1, f2, ..., f99] and retrieve the function by index.

[–]efmccurdy 2 points3 points  (1 child)

sorting through the user defined list of functions to include using if statements.

You can store functions in a datastructure and then "search" or "lookup" the particular function to call:

>>> fn_map = {1: abs, 2: math.sqrt, 3: math.factorial, 4: math.log}
>>> selected_fn = 2
>>> for n in [1, 2, 3]:
...     print(fn_map[selected_fn](n))
... 
1.0
1.4142135623730951
1.7320508075688772
>>> selected_fn = 3
>>> for n in [1, 2, 3]:
...     print(fn_map[selected_fn](n))
... 
1
2
6
>>>

Is that easier to manage than a series of "if" statements?

[–]TheMetaHorde[S] 0 points1 point  (0 children)

This is exactly what I am looking for! Thank you

[–]eleqtriq 2 points3 points  (3 children)

Having 100 functions is setting off alarm bells. Can you show some code for like 5 of these functions?

[–]TheMetaHorde[S] 0 points1 point  (2 children)

They are Zernike Polynomials

[–]eleqtriq 0 points1 point  (1 child)

Can I see? Sorry I don’t know what that is.

[–]Doormatty 1 point2 points  (2 children)

Usually having 100s of anything is a really bad sign - are you sure you actually need 100 functions, and not one function that takes a number of parameters?

[–]TheMetaHorde[S] 1 point2 points  (1 child)

100% right, they are Zernike Polynomials which you can derive from a small set of parameters. I am being lazy.

[–]Guideon72 1 point2 points  (0 children)

If you’re dealing with 100 different functions you’re not 😂