[SOLVED] How to pass a function as an argument for a class member that the Class will use in a method to repeatedly evaluate that member?

Issue

I have instances of that class calling a function defined elsewhere in the program as its member, and am trying to figure out the notation to initialize the class with a member function that can be called later. Currently, initializing the object initializes the member to the evaluation of that function at the time of that objects instantiation. How do I pass the function to init() in a way that the function itself is the object member, rather than the return value of the function?

Example:

class MyClass:
    def __init__(MyFunc):
        function = MyFunc()
        
        
        
MyFuncDef():
    #Do Something
    
def main():
    
    MyObj = MyClass(MyFuncDef())
    MyObj.function()

if __name__ == 'main':
    main()

Solution

The short answer is yes, you can do it by removing the () when you’re passing the function.

When you write the () you’re calling a function obtaining its return value, whereas, without it, you pass the function itself.

reminder_1 = MultiToast(title="Stopwatch", 
                        msg=get_message, 
                        icon_path=clock, 
                        interval=show_interval_1, 
                        count=-1, 
                        runonstart=True, 
                        duration=show_duration)

Answered By – Ignacio Vergara Kausel

Answer Checked By – Gilberto Lyons (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *