367
Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.
Tôi có mã này tính toán khoảng cách giữa hai tọa độ. Hai chức năng đều nằm trong cùng một lớp.
Tuy nhiên, làm cách nào để gọi hàm distToPoint
trong hàm isNear
?
class Coordinates:
def distToPoint[self, p]:
"""
Use pythagoras to find distance
[a^2 = b^2 + c^2]
"""
...
def isNear[self, p]:
distToPoint[self, p]
...
Hỏi ngày 11 tháng 4 năm 2011 lúc 0:20Apr 11, 2011 at 0:20
0
Vì đây là các chức năng thành viên, hãy gọi nó là chức năng thành viên trong trường hợp, self
.
def isNear[self, p]:
self.distToPoint[p]
...
Đã trả lời ngày 11 tháng 4 năm 2011 lúc 0:24Apr 11, 2011 at 0:24
Jeff Mercadojeff MercadoJeff Mercado
124K31 Huy hiệu vàng239 Huy hiệu bạc258 Huy hiệu Đồng31 gold badges239 silver badges258 bronze badges
4
Điều đó không hoạt động vì distToPoint
nằm trong lớp của bạn, vì vậy bạn cần tiền tố nó với tên lớp nếu bạn muốn tham khảo nó, như thế này: classname.distToPoint[self, p]
. Bạn không nên làm điều đó như vậy, mặc dù. Một cách tốt hơn để làm điều đó là tham khảo trực tiếp phương thức thông qua thể hiện lớp [đây là đối số đầu tiên của phương thức lớp], như vậy:
def isNear[self, p]:
self.distToPoint[p]
...
0.Đã trả lời ngày 11 tháng 4 năm 2011 lúc 0:24Apr 11, 2011 at 0:24
Jeff Mercadojeff MercadoAleksi Torhamo
124K31 Huy hiệu vàng239 Huy hiệu bạc258 Huy hiệu Đồng2 gold badges32 silver badges42 bronze badges
6
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 isNear[self, p]:
self.distToPoint[p]
...
1 def isNear[self, p]:
self.distToPoint[p]
...
2def isNear[self, p]:
self.distToPoint[p]
...
3def isNear[self, p]:
self.distToPoint[p]
...
4 def isNear[self, p]:
self.distToPoint[p]
...
5def isNear[self, p]:
self.distToPoint[p]
...
6 def isNear[self, p]:
self.distToPoint[p]
...
7def isNear[self, p]:
self.distToPoint[p]
...
1 def isNear[self, p]:
self.distToPoint[p]
...
9def isNear[self, p]:
self.distToPoint[p]
...
3Sum of the Square of List of Numbers: 3851
Sum of the Square of List of Numbers: 3852
Sum of the Square of List of Numbers: 3853
def isNear[self, p]:
self.distToPoint[p]
...
3Sum 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: 3857
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
Sum of the Square of List of Numbers: 3852
Function2 : Hello Function1 : World3
Function2 : Hello Function1 : World0____21
Function2 : Hello Function1 : World6
Sum of the Square of List of Numbers: 3852
Function2 : Hello Function1 : World8
def isNear[self, p]:
self.distToPoint[p]
...
3def isNear[self, p]:
self.distToPoint[p]
...
4 Sum of the Square of List of Numbers: 3851
Các
isNear
5
Sum of the Square of List of Numbers: 3852
isNear
7isNear
8Đầu ra: & nbsp;
Sum of the Square of List of Numbers: 385
Tôi có thể gọi một chức năng bên trong một lớp trăn lớp không?
In the below example, the class method Function1 calls method Function2 from the class.
Python3
Các hàm được xác định bởi một lớp có thể được gọi trong các định nghĩa lớp như các phương thức cá thể bằng cách sử dụng bản thân.
def isNear[self, p]:
self.distToPoint[p]
...
3def isNear[self, p]:
self.distToPoint[p]
...
1 distToPoint
2self
distToPoint
4Function2 : Hello Function1 : World0
self
distToPoint
7Sum of the Square of List of Numbers: 3852
distToPoint
9Function2 : Hello Function1 : World0
self
classname.distToPoint[self, p]
2Sum of the Square of List of Numbers: 3852
classname.distToPoint[self, p]
4def isNear[self, p]:
self.distToPoint[p]
...
3def isNear[self, p]:
self.distToPoint[p]
...
1 classname.distToPoint[self, p]
7self
distToPoint
4Function2 : Hello Function1 : World0
self
def isNear[self, p]:
self.distToPoint[p]
...
02Làm thế nào để bạn gọi một chức năng bên trong Python?
Function2 : Hello Function1 : World0
def isNear[self, p]:
self.distToPoint[p]
...
4def isNear[self, p]:
self.distToPoint[p]
...
3def isNear[self, p]:
self.distToPoint[p]
...
1 def isNear[self, p]:
self.distToPoint[p]
...
12self
distToPoint
4Function2 : Hello Function1 : World0
self
2def isNear[self, p]:
self.distToPoint[p]
...
17self
def isNear[self, p]:
self.distToPoint[p]
...
19Function2 : Hello Function1 : World0
def isNear[self, p]:
self.distToPoint[p]
...
4Khi một hàm được tạo trong Python, chúng ta có thể gọi nó bằng cách viết function_name [] chính hoặc hàm khác/ hàm lồng nhau. Sau đây là cú pháp để gọi một hàm. Cú pháp: DEF function_name []:
def isNear[self, p]:
self.distToPoint[p]
...
22def isNear[self, p]:
self.distToPoint[p]
...
26Đầu ra: & nbsp;
Function2 : Hello Function1 : World
Gọi chức năng lớp cha từ chức năng lớp con, ví dụ dưới đây, phương thức lớp con gọi phương thức lớp cha. Lớp con thừa hưởng các thuộc tính từ lớp cha.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
self
8
def isNear[self, p]:
self.distToPoint[p]
...
28def isNear[self, p]:
self.distToPoint[p]
...
3def isNear[self, p]:
self.distToPoint[p]
...
1 distToPoint
2self
distToPoint
4Function2 : Hello Function1 : World0
self
distToPoint
7Sum of the Square of List of Numbers: 3852
distToPoint
9Function2 : Hello Function1 : World0
self
classname.distToPoint[self, p]
2Sum of the Square of List of Numbers: 3852
classname.distToPoint[self, p]
4def isNear[self, p]:
self.distToPoint[p]
...
3def isNear[self, p]:
self.distToPoint[p]
...
1 def isNear[self, p]:
self.distToPoint[p]
...
12self
distToPoint
4Function2 : Hello Function1 : World0
self
2def isNear[self, p]:
self.distToPoint[p]
...
17self
def isNear[self, p]:
self.distToPoint[p]
...
19Function2 : Hello Function1 : World0
def isNear[self, p]:
self.distToPoint[p]
...
4self
8
def isNear[self, p]:
self.distToPoint[p]
...
57def isNear[self, p]:
self.distToPoint[p]
...
3def isNear[self, p]:
self.distToPoint[p]
...
1 classname.distToPoint[self, p]
7self
distToPoint
4Function2 : Hello Function1 : World0
self
def isNear[self, p]:
self.distToPoint[p]
...
02Function2 : Hello Function1 : World0
self
2def isNear[self, p]:
self.distToPoint[p]
...
05self
def isNear[self, p]:
self.distToPoint[p]
...
07Function2 : Hello Function1 : World0
def isNear[self, p]:
self.distToPoint[p]
...
4 def isNear[self, p]:
self.distToPoint[p]
...
73Sum of the Square of List of Numbers: 3852
def isNear[self, p]:
self.distToPoint[p]
...
75def isNear[self, p]:
self.distToPoint[p]
...
76Sum of the Square of List of Numbers: 3852
def isNear[self, p]:
self.distToPoint[p]
...
78def isNear[self, p]:
self.distToPoint[p]
...
79Đầu ra: & nbsp;
Function2 : Hello Function1 : World