Tôi rất mới với Python. Tôi đã cố gắng chuyển giá trị từ phương pháp này sang phương pháp khác trong lớp. Tôi đã tìm kiếm về vấn đề này nhưng tôi không thể có được giải pháp thích hợp. Bởi vì trong mã của tôi, "nếu" là phương thức gọi lớp "on_any_event" mà đổi lại nên gọi phương thức khác của tôi là "dropbox_fn", sử dụng giá trị từ "on_any_event". Nó sẽ hoạt động, nếu phương thức "dropbox_fn" nằm ngoài lớp?
Tôi sẽ minh họa bằng mã.
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[]
Vấn đề chính ở đây là .. Tôi không thể gọi phương thức "on_any_event" mà không cần tham số sự kiện. Vì vậy, thay vì trả lại giá trị, hãy gọi "dropbox_fn" bên trong "on_any_event" sẽ là một cách tốt hơn. Có ai có thể giúp cho việc này không?
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc Functions in Python
In Python,
any written function can be called by another function. Note that this could be the most elegant way of breaking a problem into chunks of small problems. In this article, we will learn how can we call a defined function from another function with help of multiple examples.
Bàn luận
The Function which calls another Function is called Calling Function and function which is called by another Function is call
Called Function.
Điều kiện tiên quyết: Các chức năng trong Pythonin Python, bất kỳ chức năng bằng văn bản nào cũng có thể được gọi bởi một chức năng khác. Lưu ý rằng đây có thể là cách phá vỡ một vấn đề thanh lịch nhất thành các vấn đề nhỏ. Trong bài viết này, chúng ta sẽ tìm hiểu làm thế nào chúng ta có thể gọi một hàm được xác định từ một hàm khác với sự trợ giúp của nhiều ví dụ. & NBSP;
A stack data structure is used during the execution of the function calls. Whenever a function is invoked then the calling function is pushed into the stack and called function is executed. When the called function completes its execution and returns then the calling function is popped from the stack and executed. Calling Function execution will be completed only when called Function is execution
completes.
Gọi và gọi chức năng? & Nbsp; Hàm gọi một hàm khác được gọi là hàm và hàm gọi được gọi bởi một hàm khác là chức năng gọi.
Làm thế nào để thực thi chức năng hoạt động? & Nbsp; một cấu trúc dữ liệu ngăn xếp được sử dụng trong quá trình thực hiện các cuộc gọi chức năng. Bất cứ khi nào một hàm được gọi thì hàm gọi được đẩy vào ngăn xếp và được gọi là chức năng được thực thi. Khi hàm được gọi hoàn thành thực thi và trả về thì hàm gọi được bật ra từ ngăn xếp và thực thi. Thực thi chức năng gọi sẽ chỉ được hoàn thành khi chức năng được gọi là hoàn thành thực thi.
Python3
Trong hình dưới đây. Cuộc gọi chức năng được thực hiện từ hàm chính sang chức năng1, bây giờ trạng thái của hàm chính được lưu trữ trong ngăn xếp và thực hiện hàm chính được tiếp tục khi hàm 1 trả về. FUCNTION1 gọi hàm2 Bây giờ trạng thái của hàm1 được lưu trữ ngăn xếp và thực thi hàm 1 sẽ được tiếp tục khi hàm 2 trả về. & Nbsp;
Hãy xem xét ví dụ dưới đây của cuộc gọi chức năng. Hàm hàm sumofsquares gọi hàm vuông trả về bình phương của số. & Nbsp;
def
Square[X]:
return
[X
*
Sum of the Square of List of Numbers: 3850
def
Sum of the Square of List of Numbers: 3852
Sum of the Square of List of Numbers: 3854
Sum of the Square of List of Numbers: 3855
Sum of the Square of List of Numbers: 3856
Sum of the Square of List of Numbers: 3858
Sum of the Square of List of Numbers: 3859
Function2 : Hello Function1 : World0
Function2 : Hello Function1 : World1
Function2 : Hello Function1 : World2
Function2 : Hello Function1 : World3
Function2 : Hello Function1 : World4
Sum of the Square of List of Numbers: 3855
Function2 : Hello Function1 : World6
Function2 : Hello Function1 : World3
Sum of the Square of List of Numbers: 3854
Function2 : Hello Function1 : World9
Sum of the Square of List of Numbers: 3855
Function2 : Hello Function1 : World1
return
Sum of the Square of List of Numbers: 3854
Các
Square[X]:
8
Sum of the Square of List of Numbers: 3855
0
1Đầu ra: & nbsp;
Sum of the Square of List of Numbers: 385
Làm thế nào để bạn gọi một phương thức trong một phương thức khác trong Python?
In the below example, the class method Function1 calls method Function2 from the class.
Python3
Các phương thức thể hiện được xây dựng các chức năng vào định nghĩa lớp của một đối tượng và yêu cầu một thể hiện của lớp đó được gọi. Để gọi phương thức, bạn cần đủ điều kiện chức năng với bản thân. . Ví dụ: trong một lớp có chứa các hàm đầu tiên [] và thứ hai [], first [] có thể gọi second [].
Phương pháp lớp Python có thể gọi phương thức khác không?
Function2 : Hello Function1 : World3
return
6[X
0Sum of the Square of List of Numbers: 3855
[X
2Function2 : Hello Function1 : World3
return
6[X
5Sum of the Square of List of Numbers: 3855
[X
7
def
*
0return
6return
7
Function2 : Hello Function1 : World3
return
6*
5Phương thức gọi từ một lớp khác trong một lớp khác trong Python. Chúng ta có thể gọi phương thức của một lớp khác bằng cách sử dụng tên lớp và chức năng của họ với toán tử DOT.
Function2 : Hello Function1 : World3
return
def
Sum of the Square of List of Numbers: 38505
return
6return
7Function2 : Hello Function1 : World3
5Sum of the Square of List of Numbers: 38510
return
6Sum of the Square of List of Numbers: 38512
Function2 : Hello Function1 : World3
return
Chúng ta có thể xác định một phương thức bên trong một phương thức khác trong Python không?
Sum of the Square of List of Numbers: 38515
Sum of the Square of List of Numbers: 38519
Đầu ra: & nbsp;
Function2 : Hello Function1 : World
Làm thế nào để bạn gọi một phương thức trong một phương thức khác trong Python?parent class Function from Child class Function –
Consider the below example the child class method invokes the parent class
method. The child class inherits the attributes from the parent class.
Python3
return
1
Sum of the Square of List of Numbers: 38521
def
return
5return
6__
Function2 : Hello Function1 : World3
return
6[X
0Sum of the Square of List of Numbers: 3855
[X
2Function2 : Hello Function1 : World3
return
6[X
5Sum of the Square of List of Numbers: 3855
[X
7
def
Sum of the Square of List of Numbers: 38505
return
6return
7Function2 : Hello Function1 : World3
5Sum of the Square of List of Numbers: 38510
return
6Sum of the Square of List of Numbers: 38512
Function2 : Hello Function1 : World3
return
return
1
Sum of the Square of List of Numbers: 38550
def
*
0return
6return
7
Function2 : Hello Function1 : World3
return
6*
5Function2 : Hello Function1 : World3
5*
8return
6Sum of the Square of List of Numbers: 38500
Function2 : Hello Function1 : World3
return
Sum of the Square of List of Numbers: 38566
Sum of the Square of List of Numbers: 3855
Sum of the Square of List of Numbers: 38568
Sum of the Square of List of Numbers: 38569
Sum of the Square of List of Numbers: 3855
Sum of the Square of List of Numbers: 38571
Sum of the Square of List of Numbers: 38572
Đầu ra: & nbsp;
Function2 : Hello Function1 : World