小编kir*_*rti的帖子

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

我是python的新手.我试图将值从一个方法传递到类中的另一个方法.我搜索了这个问题,但我找不到合适的解决方案.因为在我的代码中,"if"正在调用类的方法"on_any_event",反过来应该调用另一个方法"dropbox_fn",它使用"on_any_event"中的值.如果"dropbox_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 …
Run Code Online (Sandbox Code Playgroud)

python methods class event-handling python-watchdog

22
推荐指数
2
解决办法
7万
查看次数

标签 统计

class ×1

event-handling ×1

methods ×1

python ×1

python-watchdog ×1