在Python中从同一类中的另一个方法调用一个方法

在Python中从同一类中的另一个方法调用一个方法,python,class,methods,event-handling,python-watchdog,Python,Class,Methods,Event Handling,Python Watchdog,我对python非常陌生。我试图在类中将值从一个方法传递到另一个方法。我仔细研究了这个问题,但找不到适当的解决办法。因为在我的代码中,“if”正在调用类的方法“on_any_event”,作为回报,它应该调用我的另一个方法“dropbox_fn”,该方法使用“on_any_event”中的值。如果“dropbox\u fn”方法在类之外,它会工作吗 我将用代码进行说明 class MyHandler(FileSystemEventHandler): def on_any_event(self,

我对python非常陌生。我试图在类中将值从一个方法传递到另一个方法。我仔细研究了这个问题,但找不到适当的解决办法。因为在我的代码中,“if”正在调用类的方法“on_any_event”,作为回报,它应该调用我的另一个方法“dropbox_fn”,该方法使用“on_any_event”中的值。如果“dropbox\u fn”方法在类之外,它会工作吗

我将用代码进行说明

class MyHandler(FileSystemEventHandler):
 def on_any_event(self, event):
    srcpath=event.src_path
    print (srcpath, 'has been ',event.event_type)
    print (datetime.datetime.now())
    #print srcpath.split(' ', 12 );
    filename=srcpath[12:]
    return filename # I tried to call the method. showed error like not callable 

 def dropbox_fn(self)# Or will it work if this methos is outside the class ?
    #this method uses "filename"

if __name__ == "__main__":
  path = sys.argv[1] if len(sys.argv) > 1 else '.'
  print ("entry")
  event_handler = MyHandler()
  observer = Observer()
  observer.schedule(event_handler, path, recursive=True)
  observer.start()
  try:
     while True:
         time.sleep(1)
  except KeyboardInterrupt:
    observer.stop()
  observer.join()

这里的主要问题是。。没有事件参数,我无法调用“on_any_event”方法。因此,与其返回值,不如在“on_any_event”中调用“dropbox_fn”。有人能帮忙吗?

要调用该方法,您需要使用
self来限定函数。
。此外,如果要传递文件名,请添加
filename
参数(或其他所需名称)

类MyHandler(FileSystemEventHandler):
任何事件(自身、事件)上的def:
srcpath=event.src\u路径
打印(srcpath,“已”,事件。事件类型)
打印(datetime.datetime.now())
filename=srcpath[12:]

self.dropbox_fn(filename)#访问从一个作用域到另一个作用域的成员函数或变量(在您的情况下,从一个方法到另一个方法,我们需要用类对象引用方法或变量。您可以通过引用self关键字(称为类对象)来实现

class YourClass():

    def your_function(self, *args):

        self.callable_function(param) # if you need to pass any parameter

    def callable_function(self, *params): 
        print('Your param:', param)

因此,在“dropbox\u fn”中,我可以直接使用“filename”而不是“self.name=filename”???好的,它工作了!!。“dropbox\u fn”成功地从“on\u any\u event”中获取了文件名。对于那些从谷歌来这里搜索规范副本的人:
class YourClass():

    def your_function(self, *args):

        self.callable_function(param) # if you need to pass any parameter

    def callable_function(self, *params): 
        print('Your param:', param)