0

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()
  • So you just want to send `msg=get_message` instead of `msg=get_message()`? Yes, you can do that. – zvone Jan 11 '21 at 23:05
  • I’m voting to close this question because the code is not distilling the issue down to a minimal example. – Ignacio Vergara Kausel Jan 11 '21 at 23:07
  • True, but for his first post, certainly better to err on the side of too much info, given how many questions we get with too little detail. :-) – John Mee Jan 11 '21 at 23:12
  • Certainly, that's why I still decided to provide an answer. – Ignacio Vergara Kausel Jan 11 '21 at 23:19
  • Thanks for the reply! Can I ask what you mean by 'not distilling the issue down to a minimal example?'' Want to make sure I am posting correctly in the future. –  Jan 12 '21 at 00:12
  • @BigTruss here you're mixing the logic of you application with the question how to pass a function as an argument to another function. Nothing about the multi toast is relevant and you could have made an example much smaller, probably even shorter than 10 lines, which shows the problem you're having. For example see this, https://stackoverflow.com/questions/6289646/python-function-as-a-function-argument which is basically your question in a very succint way. – Ignacio Vergara Kausel Jan 12 '21 at 09:13

1 Answers1

0

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)
John Mee
  • 50,179
  • 34
  • 152
  • 186
Ignacio Vergara Kausel
  • 5,521
  • 4
  • 31
  • 41
  • 1
    _"When you write the `()`"_, you are **calling** the function and getting its return value. Not _"creating"_ a function. – zvone Jan 11 '21 at 23:08
  • Wow... that's a simple solution. Thanks for the quick reply! Sorry , still getting used to Python syntax so didn't realize this was such a small problem. –  Jan 12 '21 at 00:10
  • @BigTruss if you're satisfied with the answer then accept it as such. – Ignacio Vergara Kausel Jan 12 '21 at 09:09