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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25825693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:42:03  来源:igfitidea点击:

Calling one method from another within same class in Python

pythonclassmethodsevent-handlingpython-watchdog

提问by kirti

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?

我对python很陌生。我试图将值从一种方法传递到类中的另一种方法。我搜索了这个问题,但我无法得到正确的解决方案。因为在我的代码中,“if”正在调用类的方法“on_any_event”,作为回报,它应该调用我的另一个方法“dropbox_fn”,它使用来自“on_any_event”的值。如果“dropbox_fn”方法在类之外,它会起作用吗?

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?

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

采纳答案by falsetru

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

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

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)

回答by Projesh Bhoumik

To accessing member functions or variables from one scope to another scope (In your case one method to another method we need to refer method or variable with class object. and you can do it by referring with self keyword which refer as class object.

从一个范围到另一个范围访问成员函数或变量(在你的情况下,一个方法到另一个方法,我们需要用类对象引用方法或变量。你可以通过引用作为类对象的 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)