Dictionary value as function to be called when key is accessed, without using “()”
问题 I have a dictionary that has values sometimes as strings, and sometimes as a functions. For the values that are functions is there a way to execute the function without explicitly typing () when the key is accessed? Example: d = {1: "A", 2: "B", 3: fn_1} d[3]() # To run function I want: d = {1: "A", 2: "B", 3: magic(fn_1)} d[3] # To run function 回答1: Another possible solution, is to create a custom dictionary object that implements this behavior: >>> class CallableDict(dict): ... def _