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ã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[]
1from testClass import MathsOperations
xyz = MathsOperations[2, 3]
print xyz.testAddition[]
2 from testClass import MathsOperations
xyz = MathsOperations[2, 3]
print xyz.testAddition[]
3def
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
4class MathsOperations:
def testAddition [x, y]:
return x + y
def testMultiplication [a, b]:
return a * b
5class MathsOperations:
def testAddition [x, y]:
return x + y
def testMultiplication [a, b]:
return a * b
6class MathsOperations:
def testAddition [x, y]:
return x + y
def testMultiplication [a, b]:
return a * b
7from 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
9class MathsOperations:
def testAddition [x, y]:
return x + y
def testMultiplication [a, b]:
return a * b
6from testClass import MathsOperations
xyz = MathsOperations[2, 3]
print xyz.testAddition[]
7 from testClass import MathsOperations
xyz = MathsOperations[]
print xyz.testAddition[2, 3]
2from 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[]
7Các
Function2 : Hello Function1 : World1
from testClass import MathsOperations
xyz = MathsOperations[2, 3]
print xyz.testAddition[]
8 Function2 : Hello Function1 : World3
Function2 : Hello Function1 : World4
Function2 : Hello Function1 : World5
from testClass import MathsOperations
xyz = MathsOperations[2, 3]
print xyz.testAddition[]
8 Function2 : Hello Function1 : World7
Function2 : Hello Function1 : World8
Function2 : Hello Function1 : World9
from testClass import MathsOperations
xyz = MathsOperations[2, 3]
print xyz.testAddition[]
7 def
1def
2 def
3Đầ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
def
4 def
5
def
def
8def
9______
class MathsOperations:
def testAddition [x, y]:
return x + y
def testMultiplication [a, b]:
return a * b
6def
9Square[X]:
3from testClass import MathsOperations
xyz = MathsOperations[2, 3]
print xyz.testAddition[]
8Square[X]:
5class MathsOperations:
def testAddition [x, y]:
return x + y
def testMultiplication [a, b]:
return a * b
6def
9Square[X]:
8from testClass import MathsOperations
xyz = MathsOperations[2, 3]
print xyz.testAddition[]
8
0
def
3def
9Square[X]:
0
class MathsOperations:
def testAddition [x, y]:
return x + y
def testMultiplication [a, b]:
return a * b
6def
9
8class MathsOperations:
def testAddition [x, y]:
return x + y
def testMultiplication [a, b]:
return a * b
6Function2 : Hello Function1 : World8
from testClass import MathsOperations
xyz = MathsOperations[2, 3]
print xyz.testAddition[]
01def
9from testClass import MathsOperations
xyz = MathsOperations[2, 3]
print xyz.testAddition[]
03class MathsOperations:
def testAddition [x, y]:
return x + y
def testMultiplication [a, b]:
return a * b
6from testClass import MathsOperations
xyz = MathsOperations[2, 3]
print xyz.testAddition[]
0
def
from testClass import MathsOperations
xyz = MathsOperations[2, 3]
print xyz.testAddition[]
08def
9Square[X]:
0class MathsOperations:
def testAddition [x, y]:
return x + y
def testMultiplication [a, b]:
return a * b
6Function2 : Hello Function1 : World8
from testClass import MathsOperations
xyz = MathsOperations[2, 3]
print xyz.testAddition[]
13def
9from testClass import MathsOperations
xyz = MathsOperations[2, 3]
print xyz.testAddition[]
15class MathsOperations:
def testAddition [x, y]:
return x + y
def testMultiplication [a, b]:
return a * b
6from testClass import MathsOperations
xyz = MathsOperations[2, 3]
print xyz.testAddition[]
0from 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[]
20from testClass import MathsOperations
xyz = MathsOperations[2, 3]
print xyz.testAddition[]
18from 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
def
4
from testClass import MathsOperations
xyz = MathsOperations[2, 3]
print xyz.testAddition[]
24
def
def
8def
9______
class MathsOperations:
def testAddition [x, y]:
return x + y
def testMultiplication [a, b]:
return a * b
6def
9Square[X]:
3from testClass import MathsOperations
xyz = MathsOperations[2, 3]
print xyz.testAddition[]
8Square[X]:
5class MathsOperations:
def testAddition [x, y]:
return x + y
def testMultiplication [a, b]:
return a * b
6def
9Square[X]:
8from testClass import MathsOperations
xyz = MathsOperations[2, 3]
print xyz.testAddition[]
8
0
def
from testClass import MathsOperations
xyz = MathsOperations[2, 3]
print xyz.testAddition[]
08def
9Square[X]:
0class MathsOperations:
def testAddition [x, y]:
return x + y
def testMultiplication [a, b]:
return a * b
6Function2 : Hello Function1 : World8
from testClass import MathsOperations
xyz = MathsOperations[2, 3]
print xyz.testAddition[]
13def
9from testClass import MathsOperations
xyz = MathsOperations[2, 3]
print xyz.testAddition[]
15class MathsOperations:
def testAddition [x, y]:
return x + y
def testMultiplication [a, b]:
return a * b
6from testClass import MathsOperations
xyz = MathsOperations[2, 3]
print xyz.testAddition[]
0from 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
3def
9Square[X]:
0
class MathsOperations:
def testAddition [x, y]:
return x + y
def testMultiplication [a, b]:
return a * b
6def
9
8class MathsOperations:
def testAddition [x, y]:
return x + y
def testMultiplication [a, b]:
return a * b
6from testClass import MathsOperations
xyz = MathsOperations[2, 3]
print xyz.testAddition[]
0class MathsOperations:
def testAddition [x, y]:
return x + y
def testMultiplication [a, b]:
return a * b
6from testClass import MathsOperations
xyz = MathsOperations[2, 3]
print xyz.testAddition[]
0
def
from testClass import MathsOperations
xyz = MathsOperations[2, 3]
print xyz.testAddition[]
08def
9Square[X]:
0from 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[]
20from testClass import MathsOperations
xyz = MathsOperations[2, 3]
print xyz.testAddition[]
75Đầu ra: & nbsp;
Function2 : Hello Function1 : World