Hướng dẫn how to call a function inside class python - cách gọi một hàm bên trong lớp python

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ướng dẫn how to call a function inside class python - cách gọi một hàm bên trong lớp python

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

  • Đọc
  • Bàn luận
  • 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.

    Hướng dẫn how to call a function inside class python - cách gọi một hàm bên trong lớp python

    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)
        ...
    
    2

    def isNear(self, p):
        self.distToPoint(p)
        ...
    
    3
    def isNear(self, p):
        self.distToPoint(p)
        ...
    
    4
    def isNear(self, p):
        self.distToPoint(p)
        ...
    
    5
    def isNear(self, p):
        self.distToPoint(p)
        ...
    
    6
    def isNear(self, p):
        self.distToPoint(p)
        ...
    
    7

    def isNear(self, p):
        self.distToPoint(p)
        ...
    
    1
    def isNear(self, p):
        self.distToPoint(p)
        ...
    
    9

    def isNear(self, p):
        self.distToPoint(p)
        ...
    
    3
    Sum of the Square of List of Numbers: 385 
    1
    Sum of the Square of List of Numbers: 385 
    2
    Sum of the Square of List of Numbers: 385 
    3

    def isNear(self, p):
        self.distToPoint(p)
        ...
    
    3
    Sum of the Square of List of Numbers: 385 
    5
    Sum of the Square of List of Numbers: 385 
    6
    Sum of the Square of List of Numbers: 385 
    7
    Sum of the Square of List of Numbers: 385 
    8
    Sum of the Square of List of Numbers: 385 
    9

    Function2 :  Hello
    Function1 :  World
    0
    Function2 :  Hello
    Function1 :  World
    1
    Sum of the Square of List of Numbers: 385 
    2
    Function2 :  Hello
    Function1 :  World
    3

    Function2 :  Hello
    Function1 :  World
    0____21
    Function2 :  Hello
    Function1 :  World
    6
    Sum of the Square of List of Numbers: 385 
    2
    Function2 :  Hello
    Function1 :  World
    8

    def isNear(self, p):
        self.distToPoint(p)
        ...
    
    3
    def isNear(self, p):
        self.distToPoint(p)
        ...
    
    4
    Sum of the Square of List of Numbers: 385 
    1

    Các

    isNear5

    Sum of the Square of List of Numbers: 385 
    2 isNear7isNear8

    Đầ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)
        ...
    
    3
    def isNear(self, p):
        self.distToPoint(p)
        ...
    
    1 distToPoint2selfdistToPoint4

    Function2 :  Hello
    Function1 :  World
    0selfdistToPoint7
    Sum of the Square of List of Numbers: 385 
    2distToPoint9

    Function2 :  Hello
    Function1 :  World
    0selfclassname.distToPoint(self, p)2
    Sum of the Square of List of Numbers: 385 
    2classname.distToPoint(self, p)4

    def isNear(self, p):
        self.distToPoint(p)
        ...
    
    3
    def isNear(self, p):
        self.distToPoint(p)
        ...
    
    1 classname.distToPoint(self, p)7selfdistToPoint4

    Function2 :  Hello
    Function1 :  World
    0self
    def isNear(self, p):
        self.distToPoint(p)
        ...
    
    02

    Làm thế nào để bạn gọi một chức năng bên trong Python?

    Function2 :  Hello
    Function1 :  World
    0
    def isNear(self, p):
        self.distToPoint(p)
        ...
    
    4

    def isNear(self, p):
        self.distToPoint(p)
        ...
    
    3
    def isNear(self, p):
        self.distToPoint(p)
        ...
    
    1
    def isNear(self, p):
        self.distToPoint(p)
        ...
    
    12selfdistToPoint4

    Function2 :  Hello
    Function1 :  World
    0self2
    def isNear(self, p):
        self.distToPoint(p)
        ...
    
    17self
    def isNear(self, p):
        self.distToPoint(p)
        ...
    
    19

    Function2 :  Hello
    Function1 :  World
    0
    def isNear(self, p):
        self.distToPoint(p)
        ...
    
    4

    Khi 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)
        ...
    
    22
    def 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

    self8

    def isNear(self, p):
        self.distToPoint(p)
        ...
    
    28

    def isNear(self, p):
        self.distToPoint(p)
        ...
    
    3
    def isNear(self, p):
        self.distToPoint(p)
        ...
    
    1 distToPoint2selfdistToPoint4

    Function2 :  Hello
    Function1 :  World
    0selfdistToPoint7
    Sum of the Square of List of Numbers: 385 
    2distToPoint9

    Function2 :  Hello
    Function1 :  World
    0selfclassname.distToPoint(self, p)2
    Sum of the Square of List of Numbers: 385 
    2classname.distToPoint(self, p)4

    def isNear(self, p):
        self.distToPoint(p)
        ...
    
    3
    def isNear(self, p):
        self.distToPoint(p)
        ...
    
    1
    def isNear(self, p):
        self.distToPoint(p)
        ...
    
    12selfdistToPoint4

    Function2 :  Hello
    Function1 :  World
    0self2
    def isNear(self, p):
        self.distToPoint(p)
        ...
    
    17self
    def isNear(self, p):
        self.distToPoint(p)
        ...
    
    19

    Function2 :  Hello
    Function1 :  World
    0
    def isNear(self, p):
        self.distToPoint(p)
        ...
    
    4

    self8

    def isNear(self, p):
        self.distToPoint(p)
        ...
    
    57

    def isNear(self, p):
        self.distToPoint(p)
        ...
    
    3
    def isNear(self, p):
        self.distToPoint(p)
        ...
    
    1 classname.distToPoint(self, p)7selfdistToPoint4

    Function2 :  Hello
    Function1 :  World
    0self
    def isNear(self, p):
        self.distToPoint(p)
        ...
    
    02

    Function2 :  Hello
    Function1 :  World
    0self2
    def isNear(self, p):
        self.distToPoint(p)
        ...
    
    05self
    def isNear(self, p):
        self.distToPoint(p)
        ...
    
    07

    Function2 :  Hello
    Function1 :  World
    0
    def isNear(self, p):
        self.distToPoint(p)
        ...
    
    4  

    def isNear(self, p):
        self.distToPoint(p)
        ...
    
    73
    Sum of the Square of List of Numbers: 385 
    2
    def isNear(self, p):
        self.distToPoint(p)
        ...
    
    75

    def isNear(self, p):
        self.distToPoint(p)
        ...
    
    76
    Sum of the Square of List of Numbers: 385 
    2
    def isNear(self, p):
        self.distToPoint(p)
        ...
    
    78

    def isNear(self, p):
        self.distToPoint(p)
        ...
    
    79

    Đầu ra: & nbsp; 

    Function2 :  Hello
    Function1 :  World

    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?

    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..

    Làm thế nào để bạn gọi một chức năng bên trong Python?

    Khi 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 ():by writing function_name() itself or another function/ nested function. Following is the syntax for calling a function. Syntax: def function_name():

    Bạn gọi một chức năng trong một lớp là gì?

    Khi chúng tôi gọi một hàm, chúng tôi nhận được giá trị trả lại của nó.Khi chúng tôi gọi một lớp học, chúng tôi sẽ nhận được một ví dụ về lớp học của lớp đó.Chúng tôi sử dụng cùng một cú pháp để xây dựng các đối tượng từ các lớp và để gọi các chức năng: thực tế này là lý do chính khiến từ có thể gọi được là một phần quan trọng trong từ vựng Python của chúng tôi.. When we call a class, we get an “instance” of that class. We use the same syntax for constructing objects from classes and for calling functions: this fact is the main reason the word “callable” is such an important part of our Python vocabulary.

    Làm thế nào để bạn gọi một chức năng trong cùng một lớp?

    Chương trình Java này được sử dụng để gọi phương thức trong cùng một lớp.Ví dụ: Lớp công khai CallingMethodSInsAmeclass {// Phương thức Định nghĩa thực hiện cuộc gọi đến một phương thức khác static static void main (String [] args) {method1 ();// Phương thức được gọi.public class CallingMethodsInSameClass { // Method definition performing a Call to another Method public static void main(String[] args) { Method1(); // Method being called.