def some_function():
print("This will always be the same")
def some_other_function(text):
print(text)
some_variable = "hello"
list_of_elements = [
"element_one": {
"name": "static_print",
"function": some_function
},
"element_two": {
"name": "flexible_print",
"function": some_other_function
}
]
In the above minimal code example I have several elements in a list, each representing in reality an element (of a game menu I am making with pygame). Each of these elements is a dictionary, with several key:value pairs, one of which is "function". The function then gets selectively called for a chosen element. Sometimes, however, a function stored in this key:value pair needs to be called with arguments, at which point I cannot figure out what to do.
I managed to do this with conditional statements, but this requires checking for each function separately, which circumvents the entire purpose of using this list of dicts as a flexible and generalised way of having multiple (menu) elements, each with its attributes and associated function neatly organised.
Is there an elegant way to do this, and if not what would be your personal approach?