Hướng dẫn how do you call a function inside another function in a class python? - làm thế nào để bạn gọi một hàm bên trong một hàm khác trong một lớp python?

368

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 do you call a function inside another function in a class python? - làm thế nào để bạn gọi một hàm bên trong một hàm khác trong một 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ạc259 Huy hiệu Đồng31 gold badges239 silver badges259 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ạc259 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 do you call a function inside another function in a class python? - làm thế nào để bạn gọi một hàm bên trong một hàm khác trong một 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 hàm bên trong một lớp Python chức năng khác không?
    In the below example, the class method Function1 calls method Function2 from the class.

    Python3

    Trong 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 hàm 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ỏ.

    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 hàm bên trong một lớp 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

    Các thuộc tính của các hàm hạng nhất tạo ra một chương trình để hiểu các hàm Python như một đối tượng. def myObject (văn bản): # vượt qua một đối số. # Gọi hàm bên trong hàm in (). # Gọi chức năng bằng biến STR.

    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 hàm bên trong một lớp Python chức năng khác không?

    Trong 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 hàm 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ỏ.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.

    Làm thế nào để bạn gọi một hàm bên trong một lớp trong Python?

    Các thuộc tính của các hàm hạng nhất tạo ra một chương trình để hiểu các hàm Python như một đối tượng.def myObject (văn bản): # vượt qua một đối số.# Gọi hàm bên trong hàm in ().# Gọi chức năng bằng biến STR.Call the function inside the print() function. # call the function using the str variable.

    Làm thế nào để bạn gọi một hàm từ một hàm khác trong cùng một lớp?

    Tương tự, một phương thức khác là Method2 () đang được xác định với Trình xác nhận truy cập 'công khai' và 'void' là loại trả về và bên trong phương thức đó2 () Phương thức1 () được gọi.Do đó, chương trình này cho thấy một phương thức có thể được gọi trong một phương thức khác vì cả hai đều thuộc cùng một lớp.Method2() is being defined with 'public' access specifier and 'void' as return type and inside that Method2() the Method1() is called. Hence, this program shows that a method can be called within another method as both of them belong to the same class.

    Tôi có thể gọi một hàm bên trong một hàm khác không?

    Gọi một chức năng từ bên trong được gọi là đệ quy và câu trả lời đơn giản là, vâng. and the simple answer is, yes.