0

I have a dropdown box where the user can select a method to check whether a certain string either equals or endswith another string.

I would think to use function pointers/objects as the dropdown box model and call the one currently selected, but since the methods are/would be

  1. string methods and
  2. equals doesn't exist

how do you best approach this in Python, also given that I'd have to call the methods on the column name like column.name.[selectedMethod](variableStringFromTextBox)?

(It's an option that on a GUI reads "Use XYZ where column name [equals or endswith] " + textbox with variable string.)

Thanks

Kawu
  • 13,647
  • 34
  • 123
  • 195

1 Answers1

1

If I understand you correctly, the dropdown list contains strings. An easy way to do it is define the appropriate methods, then use globals(). For example:

# check if string A ends with string B
def endswith(A, B):
   return A.endswith(B)

# check if string A equals to string B
def stringsequal(A, B):
   return A == B

Suppose the strings are A and B and the string holding the function name is S. Then

globals()[S](A, B)

Will invoke the function on A and B

globals() is a dictionary holding all global functions you defined, mapping from method names to method objects.

You can also maintain a dictionary of your own that maps from method names to the methods objects.

Israel Unterman
  • 13,158
  • 4
  • 28
  • 35