Calling one method from another within same class in Python

前端 未结 2 964
闹比i
闹比i 2020-12-03 00:59

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 m

相关标签:
2条回答
  • 2020-12-03 01:15

    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)
    
    0 讨论(0)
  • 2020-12-03 01:23

    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.

    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)
    
    0 讨论(0)
提交回复
热议问题