简体   繁体   English

Python - 在 Class 实例中调用多个外部函数

[英]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.这些动作来自 Class 外部定义的两个函数。 In Class I have defined portion of code to look for changes in file.在 Class 中,我定义了部分代码来查找文件中的更改。 I cannot figure out how to pass two functions in Class arguments.我无法弄清楚如何在 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.像上面那样调用first_action()second_action()但不在 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.出于某种原因,我需要为 function 指定*args ,它在调用时不接受任何参数。 Can someone explain why *args must be used?有人可以解释为什么必须使用*args吗?

You did it right in the init, but not in the call of "Watcher".您在 init 中做对了,但不是在“Watcher”的调用中。 To pass the function itself, instead of it's return value, you have to remove the braces.要传递 function 本身,而不是它的返回值,您必须删除大括号。

watcher = Watcher(watch_file, first_action, second_action)

And you should be fine.你应该没事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM