Hướng dẫn call function from class python - gọi hàm từ 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)"

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

Lưu bài viết

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

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. 
The Function which calls another Function is called Calling Function and function which is called by another Function is call Called Function.

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

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ướng dẫn call function from class python - gọi hàm từ lớp python

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;

Python3

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

Function2 :  Hello
Function1 :  World
5
from testClass import MathsOperations

xyz = MathsOperations(2, 3)
print xyz.testAddition()
8
Function2 :  Hello
Function1 :  World
7

Function2 :  Hello
Function1 :  World
8
Function2 :  Hello
Function1 :  World
9
from testClass import MathsOperations

xyz = MathsOperations(2, 3)
print xyz.testAddition()
7 def1def2 def3

Đầu ra: & nbsp; 

Sum of the Square of List of Numbers: 385 

Gọi chức năng từ một hàm khác trong cùng một lớp trong ví dụ dưới đây, chức năng Phương thức lớp1 gọi Phương thức Phương thức2 từ lớp.
In the below example, the class method Function1 calls method Function2 from the class.

Python3

def4 def5

    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

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
from testClass import MathsOperations

xyz = MathsOperations(2, 3)
print xyz.testAddition()
0

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

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

Đầu ra: & nbsp; 

Function2 :  Hello
Function1 :  World

Tôi có thể gọi một chức năng từ một lớp trăn lớp không?

Để gọi hàm tĩnh được xác định trước, chỉ cần nhập tên lớp theo sau là dấu chấm và tên hàm.Để gọi chức năng Java không tĩnh do người dùng hoặc được xác định trước, hãy tạo một đối tượng lớp và sau đó làm theo dấu chấm.Cú pháp để gọi hàm.. To call a user-defined or predefined non-static java function, create a class object and then follow the dot “.” syntax to call the function.

Chúng ta có thể gọi một chức năng bên ngoài từ lớp Python không?

Các biến được xác định bên trong lớp nhưng bên ngoài phương thức có thể được truy cập trong lớp (tất cả các phương thức bao gồm) bằng cách sử dụng thể hiện của một lớp.Ví dụ - bản thân.var_name.Nếu bạn muốn sử dụng biến đó ngay cả bên ngoài lớp, bạn phải khai báo biến đó là toàn cầu.

Bạn có thể gọi các phương thức trong __ init __ không?

Gọi các phương thức khác từ phương thức __init__, chúng ta có thể gọi các phương thức khác của lớp từ phương thức __init__ bằng cách sử dụng từ khóa tự.Mã trên sẽ in đầu ra sau.We can call other methods of the class from the __init__ method by using the self keyword. The above code will print the following output.