79124878

Date: 2024-10-25 08:37:49
Score: 0.5
Natty:
Report link

I realise that this is an old question, but I was looking for help and came across it so I thought I would include an answer for future searchers....

What I wanted to (a) group some private functions under a class, and (b) use a dictionary to choose which function to call at run-time. The step I was missing was to make each method a static method. The prototype below shows the functionality:

class Test():

    @staticmethod
    def Foo():
        return 1

    @staticmethod
    def Baa():
        return 2

    my_dict = {"foo_val": Foo,"baa_val": Baa}

    def __init__(self):
        pass

to call it I use

my_funct = Test()
print(my_funct.my_dict["baa_val"]())
Reasons:
  • Blacklisted phrase (1): looking for help
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: Stephen Ellwood