Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
functions in a dictionary
#8
I like doing:
super_func = {
  'add': sum,
  'foo': lambda a:a*a+a
}
I wouldn't delete it like this... because you want to use it in the future
del super_func["foo"]
So what I would do is maybe create a super function object!
class SuperFunction:
    def __init__(self, function, use=True):
        self.function = function
        self.in_use = use
    def run(self)
        if self.in_use:
            self.function()
        else:
            print("Failed to run function because today you can't do it")
            import inspect
            print(inspect.getsource(self.function))

#then create a dict.

super_func = {
    'add': SuperFunction(sum)
    'foo': SuperFunction(lambda a:a*a+a, False)
}

super_func["foo"].run()

# I think we can add a method better than .run() which does a function overload on the the call parenthesis for an object. I just don't know what i should google to find this.
Reply


Messages In This Thread
functions in a dictionary - by Skaperen - Apr-25-2019, 11:05 PM
RE: functions in a dictionary - by woooee - Apr-26-2019, 12:25 AM
RE: functions in a dictionary - by Skaperen - Apr-26-2019, 03:45 AM
RE: functions in a dictionary - by Skaperen - Apr-27-2019, 12:24 AM
RE: functions in a dictionary - by perfringo - Apr-26-2019, 06:21 AM
RE: functions in a dictionary - by DeaD_EyE - Apr-26-2019, 06:34 AM
RE: functions in a dictionary - by rxndy - Apr-27-2019, 02:25 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  functions and dictionary spalisetty06 3 1,936 Aug-22-2020, 04:50 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020