Hướng dẫn how to call function in class python - cách gọi hàm trong lớp python

EDIT2: Cảm ơn tất cả các bạn đã giúp đỡ! Chỉnh sửa: Khi thêm @staticmethod, nó hoạt động. Tuy nhiên tôi vẫn đang tự hỏi tại sao tôi nhận được một lỗi loại ở đây.

Tôi mới bắt đầu rất tiếc và hoàn toàn mới với nó. Tôi có một câu hỏi rất cơ bản về các cách khác nhau mà tôi có thể gọi là một chức năng từ một lớp. Tôi có tệp testclass.py với mã:

class MathsOperations:
    def __init__ [self, x, y]:
        self.a = x
        self.b = y
    def testAddition [self]:
        return [self.a + self.b]

    def testMultiplication [self]:
        return [self.a * self.b]

Tôi đang gọi lớp này từ một tệp khác được gọi là Main.py với mã sau:

from testClass import MathsOperations

xyz = MathsOperations[2, 3]
print xyz.testAddition[]

Điều này hoạt động mà không có bất kỳ vấn đề. Tuy nhiên, tôi muốn sử dụng lớp một cách đơn giản hơn nhiều.

Bây giờ tôi đã đặt mã sau trong tệp TestClass.py. Tôi đã bỏ chức năng INIT lần này.

class MathsOperations:
    def testAddition [x, y]:
        return x + y

    def testMultiplication [a, b]:
        return a * b

Gọi cái này bằng cách sử dụng;

from testClass import MathsOperations
xyz = MathsOperations[]
print xyz.testAddition[2, 3]

Điều này không hoạt động. Ai đó có thể giải thích những gì đang xảy ra sai trong trường hợp 2? Làm cách nào để sử dụng lớp này?

Lỗi tôi nhận được là "TypeError: testAddition [] có chính xác 2 đối số [3 đã cho]"

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.

    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]:

        ____10

    from testClass import MathsOperations
    
    xyz = MathsOperations[2, 3]
    print xyz.testAddition[]
    
    1
    from testClass import MathsOperations
    
    xyz = MathsOperations[2, 3]
    print xyz.testAddition[]
    
    2
    from testClass import MathsOperations
    
    xyz = MathsOperations[2, 3]
    print xyz.testAddition[]
    
    3

    def

    from testClass import MathsOperations
    
    xyz = MathsOperations[2, 3]
    print xyz.testAddition[]
    
    5

        

    from testClass import MathsOperations
    
    xyz = MathsOperations[2, 3]
    print xyz.testAddition[]
    
    7
    from testClass import MathsOperations
    
    xyz = MathsOperations[2, 3]
    print xyz.testAddition[]
    
    8
    from testClass import MathsOperations
    
    xyz = MathsOperations[2, 3]
    print xyz.testAddition[]
    
    9

        ____21

    class MathsOperations:
        def testAddition [x, y]:
            return x + y
    
        def testMultiplication [a, b]:
            return a * b
    
    22____23
    class MathsOperations:
        def testAddition [x, y]:
            return x + y
    
        def testMultiplication [a, b]:
            return a * b
    
    4
    class MathsOperations:
        def testAddition [x, y]:
            return x + y
    
        def testMultiplication [a, b]:
            return a * b
    
    5

    class MathsOperations:
        def testAddition [x, y]:
            return x + y
    
        def testMultiplication [a, b]:
            return a * b
    
    6
    class MathsOperations:
        def testAddition [x, y]:
            return x + y
    
        def testMultiplication [a, b]:
            return a * b
    
    7
    from testClass import MathsOperations
    
    xyz = MathsOperations[2, 3]
    print xyz.testAddition[]
    
    8
    class MathsOperations:
        def testAddition [x, y]:
            return x + y
    
        def testMultiplication [a, b]:
            return a * b
    
    9

    class MathsOperations:
        def testAddition [x, y]:
            return x + y
    
        def testMultiplication [a, b]:
            return a * b
    
    6
    from testClass import MathsOperations
    
    xyz = MathsOperations[2, 3]
    print xyz.testAddition[]
    
    7
    from testClass import MathsOperations
    xyz = MathsOperations[]
    print xyz.testAddition[2, 3]
    
    2
    from testClass import MathsOperations
    
    xyz = MathsOperations[2, 3]
    print xyz.testAddition[]
    
    8
    from testClass import MathsOperations
    xyz = MathsOperations[]
    print xyz.testAddition[2, 3]
    
    4

        ____10

    from testClass import MathsOperations
    
    xyz = MathsOperations[2, 3]
    print xyz.testAddition[]
    
    7

    Các

    Function2 :  Hello
    Function1 :  World
    1
    from testClass import MathsOperations
    
    xyz = MathsOperations[2, 3]
    print xyz.testAddition[]
    
    8
    Function2 :  Hello
    Function1 :  World
    3
    Function2 :  Hello
    Function1 :  World
    4

    Đầu ra: & nbsp; 

    Sum of the Square of List of Numbers: 385 

    Function2 :  Hello
    Function1 :  World
    5
    from testClass import MathsOperations
    
    xyz = MathsOperations[2, 3]
    print xyz.testAddition[]
    
    8
    Function2 :  Hello
    Function1 :  World
    7

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

    Python3

    Function2 :  Hello
    Function1 :  World
    8
    Function2 :  Hello
    Function1 :  World
    9
    from testClass import MathsOperations
    
    xyz = MathsOperations[2, 3]
    print xyz.testAddition[]
    
    7 def1def2 def3

        def def8def9______

    class MathsOperations:
        def testAddition [x, y]:
            return x + y
    
        def testMultiplication [a, b]:
            return a * b
    
    6def9Square[X]:3
    from testClass import MathsOperations
    
    xyz = MathsOperations[2, 3]
    print xyz.testAddition[]
    
    8Square[X]:5

    class MathsOperations:
        def testAddition [x, y]:
            return x + y
    
        def testMultiplication [a, b]:
            return a * b
    
    6def9Square[X]:8
    from testClass import MathsOperations
    
    xyz = MathsOperations[2, 3]
    print xyz.testAddition[]
    
    8    0

        def     3def9Square[X]:0

    class MathsOperations:
        def testAddition [x, y]:
            return x + y
    
        def testMultiplication [a, b]:
            return a * b
    
    6def9    8

    class MathsOperations:
        def testAddition [x, y]:
            return x + y
    
        def testMultiplication [a, b]:
            return a * b
    
    6
    Function2 :  Hello
    Function1 :  World
    8
    from testClass import MathsOperations
    
    xyz = MathsOperations[2, 3]
    print xyz.testAddition[]
    
    01def9
    from testClass import MathsOperations
    
    xyz = MathsOperations[2, 3]
    print xyz.testAddition[]
    
    03

    class MathsOperations:
        def testAddition [x, y]:
            return x + y
    
        def testMultiplication [a, b]:
            return a * b
    
    6
    from testClass import MathsOperations
    
    xyz = MathsOperations[2, 3]
    print xyz.testAddition[]
    
    0

        def

    from testClass import MathsOperations
    
    xyz = MathsOperations[2, 3]
    print xyz.testAddition[]
    
    08def9Square[X]:0

    class MathsOperations:
        def testAddition [x, y]:
            return x + y
    
        def testMultiplication [a, b]:
            return a * b
    
    6
    Function2 :  Hello
    Function1 :  World
    8
    from testClass import MathsOperations
    
    xyz = MathsOperations[2, 3]
    print xyz.testAddition[]
    
    13def9
    from testClass import MathsOperations
    
    xyz = MathsOperations[2, 3]
    print xyz.testAddition[]
    
    15

    class MathsOperations:
        def testAddition [x, y]:
            return x + y
    
        def testMultiplication [a, b]:
            return a * b
    
    6
    from testClass import MathsOperations
    
    xyz = MathsOperations[2, 3]
    print xyz.testAddition[]
    
    0

    from testClass import MathsOperations
    
    xyz = MathsOperations[2, 3]
    print xyz.testAddition[]
    
    18
    from testClass import MathsOperations
    
    xyz = MathsOperations[2, 3]
    print xyz.testAddition[]
    
    8
    from testClass import MathsOperations
    
    xyz = MathsOperations[2, 3]
    print xyz.testAddition[]
    
    20

    from testClass import MathsOperations
    
    xyz = MathsOperations[2, 3]
    print xyz.testAddition[]
    
    18
    from testClass import MathsOperations
    
    xyz = MathsOperations[2, 3]
    print xyz.testAddition[]
    
    22

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

    def4

    from testClass import MathsOperations
    
    xyz = MathsOperations[2, 3]
    print xyz.testAddition[]
    
    24

        def def8def9______

    class MathsOperations:
        def testAddition [x, y]:
            return x + y
    
        def testMultiplication [a, b]:
            return a * b
    
    6def9Square[X]:3
    from testClass import MathsOperations
    
    xyz = MathsOperations[2, 3]
    print xyz.testAddition[]
    
    8Square[X]:5

    class MathsOperations:
        def testAddition [x, y]:
            return x + y
    
        def testMultiplication [a, b]:
            return a * b
    
    6def9Square[X]:8
    from testClass import MathsOperations
    
    xyz = MathsOperations[2, 3]
    print xyz.testAddition[]
    
    8    0

        def

    from testClass import MathsOperations
    
    xyz = MathsOperations[2, 3]
    print xyz.testAddition[]
    
    08def9Square[X]:0

    class MathsOperations:
        def testAddition [x, y]:
            return x + y
    
        def testMultiplication [a, b]:
            return a * b
    
    6
    Function2 :  Hello
    Function1 :  World
    8
    from testClass import MathsOperations
    
    xyz = MathsOperations[2, 3]
    print xyz.testAddition[]
    
    13def9
    from testClass import MathsOperations
    
    xyz = MathsOperations[2, 3]
    print xyz.testAddition[]
    
    15

    class MathsOperations:
        def testAddition [x, y]:
            return x + y
    
        def testMultiplication [a, b]:
            return a * b
    
    6
    from testClass import MathsOperations
    
    xyz = MathsOperations[2, 3]
    print xyz.testAddition[]
    
    0

    from testClass import MathsOperations
    
    xyz = MathsOperations[2, 3]
    print xyz.testAddition[]
    
    18
    from testClass import MathsOperations
    
    xyz = MathsOperations[2, 3]
    print xyz.testAddition[]
    
    8
    from testClass import MathsOperations
    
    xyz = MathsOperations[2, 3]
    print xyz.testAddition[]
    
    20

        def     3def9Square[X]:0

    Đầu ra: & nbsp;

    class MathsOperations:
        def testAddition [x, y]:
            return x + y
    
        def testMultiplication [a, b]:
            return a * b
    
    6
    Function2 :  Hello
    Function1 :  World
    8
    from testClass import MathsOperations
    
    xyz = MathsOperations[2, 3]
    print xyz.testAddition[]
    
    01def9
    from testClass import MathsOperations
    
    xyz = MathsOperations[2, 3]
    print xyz.testAddition[]
    
    03

    class MathsOperations:
        def testAddition [x, y]:
            return x + y
    
        def testMultiplication [a, b]:
            return a * b
    
    6
    from testClass import MathsOperations
    
    xyz = MathsOperations[2, 3]
    print xyz.testAddition[]
    
    0  

    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.

    def4

    from testClass import MathsOperations
    
    xyz = MathsOperations[2, 3]
    print xyz.testAddition[]
    
    24

    from testClass import MathsOperations
    
    xyz = MathsOperations[2, 3]
    print xyz.testAddition[]
    
    75

    Đầu ra: & nbsp; 

    Function2 :  Hello
    Function1 :  World

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

    Để gọi một chức năng trong lớp với Python, chúng tôi gọi chức năng với bản thân trước nó. Chúng tôi gọi phương thức thể hiện DistTopoint trong lớp tọa độ bằng cách gọi bản thân. DisTtopoint. Tự là thay đổi lưu trữ thể hiện lớp tọa độ hiện tại.call the function with self before it. We call the distToPoint instance method within the Coordinates class by calling self. distToPoint . self is variable storing the current Coordinates class instance.

    Làm cách nào để gọi chức năng trong Python?

    Sử dụng dấu ngoặc đơn để gọi một hàm trong Python để sử dụng các hàm trong Python, bạn viết tên hàm [hoặc biến trỏ đến đối tượng hàm] theo sau là dấu ngoặc đơn [để gọi hàm].write the function name [or the variable that points to the function object] followed by parentheses [to call the function].

    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.

    Bạn có thể gọi một chức năng từ một lớp khác trong Python không?

    Phươ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.Sau đó, chúng ta có thể gọi Phương thức_A từ lớp B theo cách sau: Lớp A: Phương thức_A [self]: {} Lớp B: meather_b [self]: A.we can call the method of another class by using their class name and function with dot operator. then we can call method_A from class B by following way: class A: method_A[self]: {} class B: method_B[self]: A.

    Bài Viết Liên Quan

    Chủ Đề