zwornik
zwornik

Reputation: 349

Python - call multiple external functions in Class instance

I have a script that watches for changes in file and if occurred it should trigger some actions. Those actions come from two functions defined outside Class. In Class I have defined portion of code to look for changes in file. I cannot figure out how to pass two functions in Class arguments. Here is simplified portion of my script:

import time, os

watch_file = 'my_file.txt'

def first_action():
    print('First action called')

def second_action():
    print('Second action called')

class Watcher():
    def __init__(self, watch_file, first_action=None, second_action=None):
        self._cached_stamp = 0
        self.filename = watch_file
        self.first_action = first_action
        self.second_action = second_action

    # Look for changes in 'my_file.txt'
    def look(self):
        stamp = os.stat(self.filename).st_mtime
        if stamp != self._cached_stamp:
            self._cached_stamp = stamp
            # File has changed, so do something...
            print('File changed')
            if self.first_action is not None:
                print('Call first action')
                self.first_action(self)
            if self.second_action is not None:
                print('Call second action')
                self.second_action(self)    


watcher = Watcher(watch_file, first_action(), second_action())

Doing it like above calls first_action() and second_action() but not inside Class. I know it because I dont see printed 'Call first action' or 'Call second action' I was able to correctly trigger first action with below code:

watch_file = 'my_file.txt'

def first_action():
    print('First action called')

def second_action():
    print('Second action called')

class Watcher():
    def __init__(self, watch_file, first_action=None, *args):
        self._cached_stamp = 0
        self.filename = watch_file
        self.first_action = first_action
        self.args = args

    # Look for changes in 'my_file.txt'
    def look(self):
        stamp = os.stat(self.filename).st_mtime
        if stamp != self._cached_stamp:
            self._cached_stamp = stamp
            # File has changed, so do something...
            print('File changed')
            if self.first_action is not None:
                print('Call first action')
                self.first_action(*self.args)    


watcher = Watcher(watch_file, first_action)

For some reason I need to specify *args even for function which does not take any argument on call. Can someone explain why *args must be used?

Upvotes: 0

Views: 63

Answers (1)

DerHamm
DerHamm

Reputation: 173

You did it right in the init, but not in the call of "Watcher". To pass the function itself, instead of it's return value, you have to remove the braces.

watcher = Watcher(watch_file, first_action, second_action)

And you should be fine.

Upvotes: 1

Related Questions