Hướng dẫn python call function from another function in same class - gọi hàm python từ một hàm khác trong cùng một lớp

366

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 python call function from another function in same class - gọi hàm python từ một hàm khác trong cùng một lớ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

lớp C:.

def f (tự):.

def g (tự):.

  • bản thân. f ().
  • print ("def") hàm g () gọi hàm f ().
  • lớp C:.

    def f (tự):.

    def g (tự):.

    bản thân. f (). 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. 

    print ("def") hàm g () gọi hàm f (). 
    The Function which calls another Function is called Calling Function and function which is called by another Function is call Called Function.

    class_instance = c ().
    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.

    class_instance. f ().

    Hướng dẫn python call function from another function in same class - gọi hàm python từ một hàm khác trong cùng một lớp

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

    Python3

    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 ():

    Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    Bàn luận

    Đ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;

    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.

    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

    Đầu ra: & nbsp; 

    Sum of the Square of List of Numbers: 385 

    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

    In the below example, the class method Function1 calls method Function2 from the class.

    Python3

    self8 self9

    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

    Function2 :  Hello
    Function1 :  World
    0__72

    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

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

    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

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

    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
    0__72

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

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

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

    Đầu ra: & nbsp; 

    Function2 :  Hello
    Function1 :  World

    Bạn có thể gọi một hàm trong cùng một chức năng Python không?

    Nếu bạn quen thuộc với các chức năng trong Python, thì bạn sẽ biết rằng việc gọi một hàm khác là khá phổ biến. Trong Python, một chức năng cũng có thể tự gọi! Một chức năng tự gọi được cho là đệ quy và kỹ thuật sử dụng một hàm đệ quy được gọi là đệ quy.it's quite common for one function to call another. In Python, it's also possible for a function to call itself! A function that calls itself is said to be recursive, and the technique of employing a recursive function is called recursion.

    Làm cách nào để gọi một hàm trong cùng một lớp?

    Có hai cách để gọi một hàm phương thức khác từ cùng một lớp. Đầu tiên, bạn có thể sử dụng một dấu chấm/thời gian để truy cập phương thức từ biến lớp. Thứ hai, bạn có thể chỉ cần gọi chức năng và chuyển đối tượng lớp làm đối số.use a dot/period to access the method from the class variable. Second, you can simply call the function and pass the class object as an argument.

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

    Cách gọi một phương thức thể hiện trong cùng một lớp trong Python..
    lớp C:.
    def f (tự):.
    print("abc").
    def g (tự):.
    bản thân.f ().
    print ("def") hàm g () gọi hàm f ().
    class_instance = c ().
    class_instance.f ().

    Làm thế nào để bạn gọi một chức năng từ một chức năng khác 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():