在Python中从同一个类中调用另一个方法 [英] Calling one method from another within same class in Python

查看:2187
本文介绍了在Python中从同一个类中调用另一个方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很py。。。。我正在尝试将值从一个方法传递给另一个方法。我搜索了这个问题,但我无法得到正确的解决方案。因为在我的代码中,if是调用类的on_any_event方法,因为它应该调用另一个方法dropbox_fn,它使用on_any_event的值。如果dropbox_fn方法在类之外,它会工作吗?



我将用代码说明。

 类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#我试图调用该方法。显示错误,如不可调用

def dropbox_fn(self)#或者如果这个methos在类之外,它会工作吗?
#this方法使用filename

如果__name__ ==__main__:
path = sys.argv [1]如果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)
除了KeyboardInterrupt:
observer.stop()
observer.join()

这里的主要问题是..我无法调用on_any_event方法无事件参数。所以不要返回值,在on_any_event里面调用dropbox_fn会更好一些。有人可以帮助吗?

解决方案

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



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ path $ path path path path path path path path path path path path path path path path path path path path path path path path path path path path path path path path ,'has been',event.event_type)
print(datetime.datetime.now())
filename = srcpath [12:]
self.dropbox_fn(filename)#< - -

def dropbox_fn(self,filename):#< -----
print('in dropbox_fn:',filename)


I am very new to python. I was trying to pass value from one method to another within the class. I searched about the issue but i could not get proper solution. Because in my code, "if" is calling class's method "on_any_event" that in return should call my another method "dropbox_fn", which make use of the value from "on_any_event". Will it work, if the "dropbox_fn" method is outside the class?

I will illustrate with code.

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()

The main issue in here is.. I cannot call "on_any_event" method without event parameter. So rather than returning value, calling "dropbox_fn" inside "on_any_event" would be a better way. Can someone help with this?

解决方案

To call the method, you need to qualify function with self.. In addition to that, if you want to pass a filename, add a filename parameter (or other name you want).

class MyHandler(FileSystemEventHandler):

    def on_any_event(self, event):
        srcpath = event.src_path
        print (srcpath, 'has been ',event.event_type)
        print (datetime.datetime.now())
        filename = srcpath[12:]
        self.dropbox_fn(filename) # <----

    def dropbox_fn(self, filename):  # <-----
        print('In dropbox_fn:', filename)

这篇关于在Python中从同一个类中调用另一个方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆