Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Object-Oriented Programming in Python, Object-Oriented Programming in Python | Set 2
Constructors are generally used for instantiating an object. The task of constructors is to initialize[assign values] to the data members of
the class when an object of the class is created. In Python the __init__[] method is called the constructor and is always called when an object is created.
Syntax of constructor declaration :
def __init__[self]: # body of the constructor
Bàn luận
- Điều kiện tiên quyết: Lập trình hướng đối tượng trong Python, lập trình hướng đối tượng trong Python | Đặt 2 & nbsp; các hàm tạo thường được sử dụng để khởi tạo một đối tượng. Nhiệm vụ của các hàm tạo là khởi tạo [gán giá trị] cho các thành viên dữ liệu của lớp khi một đối tượng của lớp được tạo. Trong Python, phương thức __init __ [] được gọi là hàm tạo và luôn được gọi khi một đối tượng được tạo.Syntax của khai báo hàm tạo: & nbsp; The default constructor is a simple constructor which doesn’t accept any arguments. Its definition has only one argument which is a reference to the instance being constructed.
- Các loại nhà xây dựng: & nbsp; constructor with parameters is known as parameterized constructor. The parameterized constructor takes its first argument as a reference to the instance being constructed known as self and the rest of the arguments are provided by the programmer.
Trình xây dựng mặc định: Trình xây dựng mặc định là một hàm tạo đơn giản mà không chấp nhận bất kỳ đối số nào. Định nghĩa của nó chỉ có một đối số là một tham chiếu đến trường hợp được xây dựng.
Python3
hàm tạo tham số hóa: hàm tạo với các tham số được gọi là hàm tạo tham số hóa. Hàm tạo tham số hóa lấy đối số đầu tiên của nó làm tham chiếu đến thể hiện được xây dựng được gọi là bản thân và phần còn lại của các đối số được cung cấp bởi lập trình viên.
Ví dụ về hàm tạo mặc định: & nbsp; & nbsp;
class
GeekforGeeks:
def
GeekforGeeks0
GeekforGeeks1
GeekforGeeks2
GeekforGeeks3
First number = 1000 Second number = 2000 Addition of two numbers = 30004
First number = 1000 Second number = 2000 Addition of two numbers = 30005
GeekforGeeks1
First number = 1000 Second number = 2000 Addition of two numbers = 30007
GeekforGeeks3
GeekforGeeks1
GeekforGeeks5
GeekforGeeks6
GeekforGeeks7
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.1
def
First number = 1000
Second number = 2000
Addition of two numbers = 3000
0GeekforGeeks
1GeekforGeeks
2
GeekforGeeks
First number = 1000
Second number = 2000
Addition of two numbers = 3000
8GeekforGeeks
6 Sum of list : 15
Square of int : 25
String : Hello! GeeksforGeeks.
0 parameterized constructor :
Python3
Đầu ra: & nbsp;
Ví dụ về hàm tạo phân loại: & nbsp;
class
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.3
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.5
GeekforGeeks6
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.7
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.9
GeekforGeeks6
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.7
equation 2 : -3 equation 1 : 2 equation 3 : 11.03
GeekforGeeks6
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.7
def
GeekforGeeks0
GeekforGeeks1
equation 2 : -3 equation 1 : 2 equation 3 : 11.00
GeekforGeeks3
GeekforGeeks1
equation 2 : -3 equation 1 : 2 equation 3 : 11.03
GeekforGeeks6
equation 2 : -3 equation 1 : 2 equation 3 : 11.05
GeekforGeeks3
GeekforGeeks1____58
GeekforGeeks6
class
0
def
class
3
GeekforGeeks1
GeekforGeeks2
GeekforGeeks3
First number = 1000 Second number = 2000 Addition of two numbers = 30004
First number = 1000 Second number = 2000 Addition of two numbers = 30005
class
9 GeekforGeeks:
0 GeekforGeeks:
1First number = 1000 Second number = 2000 Addition of two numbers = 30005
GeekforGeeks1
GeekforGeeks:
4GeekforGeeks3
First number = 1000 Second number = 2000 Addition of two numbers = 30004
First number = 1000 Second number = 2000 Addition of two numbers = 30005
GeekforGeeks:
8 GeekforGeeks:
0 GeekforGeeks:
1First number = 1000 Second number = 2000 Addition of two numbers = 30005
GeekforGeeks1
3GeekforGeeks3
First number = 1000 Second number = 2000 Addition of two numbers = 30004
First number = 1000 Second number = 2000 Addition of two numbers = 30005
7 GeekforGeeks:
0 GeekforGeeks:
1First number = 1000 Second number = 2000 Addition of two numbers = 30005
GeekforGeeks1
def
2
def
def
5
GeekforGeeks1
GeekforGeeks2
GeekforGeeks14
GeekforGeeks15
def
First number = 1000
Second number = 2000
Addition of two numbers = 3000
0GeekforGeeks
1GeekforGeeks
2
First number = 1000 Second number = 2000 Addition of two numbers = 3000
Điều kiện tiên quyết - Trình xây dựng, @ClassMethod DecoratorsConstructors, @classmethod decorators
Python không hỗ trợ nhiều hàm tạo rõ ràng, nhưng có một số cách sử dụng nhiều hàm tạo có thể đạt được. Nếu nhiều phương thức __init__ được viết cho cùng một lớp, thì loại mới nhất ghi đè lên tất cả các hàm tạo trước đó. Nhìn vào ví dụ dưới đây.__init__ methods are written for the same class, then the latest one overwrites all the previous constructors. Look at the example below.
Python3
class
GeekforGeeks17
def
GeekforGeeks0
GeekforGeeks1
GeekforGeeks2
GeekforGeeks3
First number = 1000 Second number = 2000 Addition of two numbers = 30004
First number = 1000 Second number = 2000 Addition of two numbers = 30005
GeekforGeeks26
GeekforGeeks13
def
GeekforGeeks0
GeekforGeeks1
GeekforGeeks2
GeekforGeeks3
First number = 1000 Second number = 2000 Addition of two numbers = 30004
First number = 1000 Second number = 2000 Addition of two numbers = 30005
GeekforGeeks36
GeekforGeeks13
def
GeekforGeeks0
GeekforGeeks1
GeekforGeeks2
GeekforGeeks3
First number = 1000 Second number = 2000 Addition of two numbers = 30004
First number = 1000 Second number = 2000 Addition of two numbers = 30005
GeekforGeeks46
GeekforGeeks13
GeekforGeeks3
First number = 1000 Second number = 2000 Addition of two numbers = 30004
First number = 1000 Second number = 2000 Addition of two numbers = 30005
GeekforGeeks36
GeekforGeeks13
GeekforGeeks
48GeekforGeeks
6 GeekforGeeks
50
Cần nhiều hàm tạo
- Nhiều hàm tạo được yêu cầu khi người ta phải thực hiện các hành động khác nhau trong việc khởi tạo một lớp. Điều này rất hữu ích khi lớp phải thực hiện các hành động khác nhau trên các tham số khác nhau. Các nhà xây dựng lớp có thể được thực hiện để thể hiện tính đa hình theo ba cách được liệt kê dưới đây.
- Quá tải các hàm tạo dựa trên các đối số.__init__.
- Gọi các phương thức từ __init__.@classmethod decorator.
Sử dụng công cụ trang trí @ClassMethod.
Bài viết này giải thích làm thế nào để có nhiều hàm tạo một cách sạch sẽ và pythonic với các ví dụ.
Quá tải các hàm tạo dựa trên các đối sốsample,
- Quá tải hàm tạo được thực hiện bằng cách kiểm tra các điều kiện cho các đối số được thông qua và thực hiện các hành động cần thiết. Ví dụ: xem xét việc chuyển một đối số cho mẫu lớp, & nbsp;int, the square of the number should be the answer.
- Nếu tham số là INT, bình phương của số phải là câu trả lời.String, the answer should be “Hello!!”+string.
- Nếu tham số là một chuỗi, câu trả lời phải là Hello Hello !!length greater than 1, the sum of arguments should be stored as the answer.
Python3
Nếu tham số có độ dài lớn hơn 1, tổng các đối số nên được lưu trữ dưới dạng câu trả lời.
class
GeekforGeeks52
def
GeekforGeeks0
GeekforGeeks1____111
GeekforGeeks58
GeekforGeeks59
GeekforGeeks3
GeekforGeeks61
GeekforGeeks62
GeekforGeeks63
GeekforGeeks64
GeekforGeeks65
GeekforGeeks66
GeekforGeeks1
GeekforGeeks68
GeekforGeeks6
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.7
GeekforGeeks66
GeekforGeeks72
GeekforGeeks73
GeekforGeeks74
GeekforGeeks75
GeekforGeeks76
GeekforGeeks1
GeekforGeeks68
GeekforGeeks:
0GeekforGeeks6
GeekforGeeks81
GeekforGeeks3
GeekforGeeks83
GeekforGeeks84
GeekforGeeks85
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.7
GeekforGeeks87
GeekforGeeks88
GeekforGeeks2
GeekforGeeks66
GeekforGeeks1
GeekforGeeks68
GeekforGeeks6
GeekforGeeks94
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.7
GeekforGeeks96
GeekforGeeks58
GeekforGeeks94
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.7__
GeekforGeeks66
GeekforGeeks1
GeekforGeeks68
GeekforGeeks6
First number = 1000 Second number = 2000 Addition of two numbers = 300013
GeekforGeeks:
0GeekforGeeks94
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.7
GeekforGeeks96
GeekforGeeks:
0First number = 1000 Second number = 2000 Addition of two numbers = 300019
First number = 1000 Second number = 2000 Addition of two numbers = 300020
GeekforGeeks6
First number = 1000 Second number = 2000 Addition of two numbers = 300022222222222222222
First number = 1000 Second number = 2000 Addition of two numbers = 30004
First number = 1000 Second number = 2000 Addition of two numbers = 30005
First number = 1000 Second number = 2000 Addition of two numbers = 300035
First number = 1000 Second number = 2000 Addition of two numbers = 300036
First number = 1000 Second number = 2000 Addition of two numbers = 300037
GeekforGeeks6
First number = 1000 Second number = 2000 Addition of two numbers = 30002222231
GeekforGeeks13
First number = 1000 Second number = 2000 Addition of two numbers = 30004
First number = 1000 Second number = 2000 Addition of two numbers = 30005
First number = 1000 Second number = 2000 Addition of two numbers = 300044
First number = 1000 Second number = 2000 Addition of two numbers = 300045
First number = 1000 Second number = 2000 Addition of two numbers = 300046
GeekforGeeks6
First number = 1000 Second number = 2000 Addition of two numbers = 30002222249
GeekforGeeks13
First number = 1000 Second number = 2000 Addition of two numbers = 30004
First number = 1000 Second number = 2000 Addition of two numbers = 30005
First number = 1000 Second number = 2000 Addition of two numbers = 300053
First number = 1000 Second number = 2000 Addition of two numbers = 300054
Đầu ra
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.
Trong mã trên, biến thể hiện là ANS, nhưng các giá trị của nó khác nhau dựa trên các đối số. Vì một số lượng các đối số cho lớp, *Args được sử dụng là một bộ có chứa các đối số được truyền và có thể được truy cập bằng cách sử dụng một chỉ mục. Trong trường hợp của int và chuỗi, chỉ có một đối số được truyền và do đó được truy cập như ARGS [0] [phần tử duy nhất trong tuple].ans, but its values differ based on the arguments. Since a variable number of arguments for the class, *args is used which is a tuple that contains the arguments passed and can be accessed using an index. In the case of int and string, only one argument is passed and thus accessed as args[0] [the only element in the tuple].
Gọi các phương thức từ __init__
Một lớp có thể có một hàm tạo __init__ có thể thực hiện bất kỳ hành động nào khi thể hiện của lớp được tạo. Hàm tạo này có thể được thực hiện cho các chức năng khác nhau thực hiện các hành động khác nhau dựa trên các đối số được thông qua. Bây giờ hãy xem xét một ví dụ: & nbsp;
- Nếu số lượng đối số được truyền là 2, thì hãy đánh giá biểu thức x = a2-b2x = a2-b2
- Nếu số lượng đối số được truyền là 3, thì hãy đánh giá biểu thức y = a2+b2-c.y = a2+b2-c.
- IFMORE so với 3 đối số đã được thông qua, sau đó tổng hợp các ô vuông, chia nó cho giá trị cao nhất trong các đối số được thông qua.more than 3 arguments have been passed, then sum up the squares, divide it by the highest value in the arguments passed.
Python3
class
First number = 1000 Second number = 2000 Addition of two numbers = 300056
def
GeekforGeeks0
GeekforGeeks1____111
GeekforGeeks58
First number = 1000 Second number = 2000 Addition of two numbers = 300063
GeekforGeeks3
GeekforGeeks61
GeekforGeeks62
First number = 1000 Second number = 2000 Addition of two numbers = 300067
GeekforGeeks6
GeekforGeeks6
First number = 1000 Second number = 2000 Addition of two numbers = 300025
GeekforGeeks65
GeekforGeeks66
GeekforGeeks1
GeekforGeeks68
GeekforGeeks6
GeekforGeeks1
First number = 1000 Second number = 2000 Addition of two numbers = 300077
GeekforGeeks3
GeekforGeeks83
GeekforGeeks62
First number = 1000 Second number = 2000 Addition of two numbers = 300067
GeekforGeeks6
GeekforGeeks6
First number = 1000 Second number = 2000 Addition of two numbers = 300027
GeekforGeeks65
GeekforGeeks66
GeekforGeeks1
GeekforGeeks68
GeekforGeeks6
GeekforGeeks1
First number = 1000 Second number = 2000 Addition of two numbers = 300091
GeekforGeeks3
First number = 1000 Second number = 2000 Addition of two numbers = 300093
GeekforGeeks65
GeekforGeeks66
GeekforGeeks1
GeekforGeeks68
GeekforGeeks6
GeekforGeeks1
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.00
def
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.03
GeekforGeeks1____
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.05
GeekforGeeks3____
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.07
GeekforGeeks6
GeekforGeeks85
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.7
GeekforGeeks96
GeekforGeeks58__
GeekforGeeks3
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.29
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.30
def
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.33
GeekforGeeks1____
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.05
GeekforGeeks3
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.37
GeekforGeeks6
GeekforGeeks85
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.7
GeekforGeeks96__
GeekforGeeks3
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.29
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.56
def
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.59
GeekforGeeks1
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.05
GeekforGeeks3
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.63
GeekforGeeks6
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.7
Các
GeekforGeeks66
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.63
GeekforGeeks:
0GeekforGeeks6
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.80
GeekforGeeks58
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.80
GeekforGeeks3
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.63
GeekforGeeks6
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.86
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.87
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.88
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.89
GeekforGeeks3
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.91
GeekforGeeks6
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.86
GeekforGeeks3
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.29
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.96
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.97
GeekforGeeks6
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.99
GeekforGeeks64
GeekforGeeks11
First number = 1000 Second number = 2000 Addition of two numbers = 300025
GeekforGeeks13
equation 2 : -3 equation 1 : 2 equation 3 : 11.004
GeekforGeeks6
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.99
GeekforGeeks64___
equation 2 : -3 equation 1 : 2 equation 3 : 11.013
GeekforGeeks6
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.99
GeekforGeeks64
GeekforGeeks11
First number = 1000 Second number = 2000 Addition of two numbers = 300025
GeekforGeeks11
First number = 1000 Second number = 2000 Addition of two numbers = 300027
GeekforGeeks11__
First number = 1000 Second number = 2000 Addition of two numbers = 30004
First number = 1000 Second number = 2000 Addition of two numbers = 30005
equation 2 : -3 equation 1 : 2 equation 3 : 11.028
equation 2 : -3 equation 1 : 2 equation 3 : 11.029
First number = 1000 Second number = 2000 Addition of two numbers = 30004
First number = 1000 Second number = 2000 Addition of two numbers = 30005
equation 2 : -3 equation 1 : 2 equation 3 : 11.032
equation 2 : -3 equation 1 : 2 equation 3 : 11.033
First number = 1000 Second number = 2000 Addition of two numbers = 30004
First number = 1000 Second number = 2000 Addition of two numbers = 30005
equation 2 : -3 equation 1 : 2 equation 3 : 11.036
equation 2 : -3 equation 1 : 2 equation 3 : 11.037
Đầu ra
equation 2 : -3 equation 1 : 2 equation 3 : 11.0
Trong mã trên, biến thể hiện là ANS, nhưng các giá trị của nó khác nhau dựa trên các đối số. Vì một số lượng các đối số cho lớp, *Args được sử dụng là một bộ có chứa các đối số được truyền và có thể được truy cập bằng cách sử dụng một chỉ mục. Trong trường hợp của int và chuỗi, chỉ có một đối số được truyền và do đó được truy cập như ARGS [0] [phần tử duy nhất trong tuple].
Gọi các phương thức từ __init__
Một lớp có thể có một hàm tạo __init__ có thể thực hiện bất kỳ hành động nào khi thể hiện của lớp được tạo. Hàm tạo này có thể được thực hiện cho các chức năng khác nhau thực hiện các hành động khác nhau dựa trên các đối số được thông qua. Bây giờ hãy xem xét một ví dụ: & nbsp;
Nếu số lượng đối số được truyền là 2, thì hãy đánh giá biểu thức x = a2-b2
Nếu số lượng đối số được truyền là 3, thì hãy đánh giá biểu thức y = a2+b2-c.
IFMORE so với 3 đối số đã được thông qua, sau đó tổng hợp các ô vuông, chia nó cho giá trị cao nhất trong các đối số được thông qua.
class
First number = 1000 Second number = 2000 Addition of two numbers = 300056classmethod is cls, which is like the selfof the instance methods. Here cls refer to the class itself. This proves to be very helpful to use multiple constructors in Python and is a more Pythonic approach considered to the above ones. Consider the same example used above. Evaluate different expressions based on the number of inputs.
Python3
class
First number = 1000 Second number = 2000 Addition of two numbers = 300056
def
GeekforGeeks0
GeekforGeeks1____111
GeekforGeeks58
First number = 1000 Second number = 2000 Addition of two numbers = 300063
GeekforGeeks3
GeekforGeeks1
GeekforGeeks68
GeekforGeeks6
equation 2 : -3 equation 1 : 2 equation 3 : 11.049
equation 2 : -3 equation 1 : 2 equation 3 : 11.051
def
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.03
equation 2 : -3 equation 1 : 2 equation 3 : 11.055____
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.05
GeekforGeeks3____
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.07
GeekforGeeks6
equation 2 : -3 equation 1 : 2 equation 3 : 11.055
equation 2 : -3 equation 1 : 2 equation 3 : 11.061
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.7
GeekforGeeks96
GeekforGeeks58__
GeekforGeeks3
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.29
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.30
equation 2 : -3 equation 1 : 2 equation 3 : 11.051
def
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.33
equation 2 : -3 equation 1 : 2 equation 3 : 11.0555
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.05
GeekforGeeks3
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.37
GeekforGeeks6
equation 2 : -3 equation 1 : 2 equation 3 : 11.055
equation 2 : -3 equation 1 : 2 equation 3 : 11.061
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.7
GeekforGeeks96__
GeekforGeeks3
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.29
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.56
equation 2 : -3 equation 1 : 2 equation 3 : 11.051
def
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.59
equation 2 : -3 equation 1 : 2 equation 3 : 11.055
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.05
GeekforGeeks3
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.63
GeekforGeeks6
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.7
Các
GeekforGeeks66
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.63
GeekforGeeks:
0GeekforGeeks6
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.80
GeekforGeeks58
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.80
GeekforGeeks3
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.63
GeekforGeeks6
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.86
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.87
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.88
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.89
GeekforGeeks3
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.91
GeekforGeeks6
equation 2 : -3 equation 1 : 2 equation 3 : 11.055
equation 2 : -3 equation 1 : 2 equation 3 : 11.051
GeekforGeeks3
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.29
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.96
equation 2 : -3 equation 1 : 2 equation 3 : 11.0555
GeekforGeeks6
equation 2 : -3 equation 1 : 2 equation 3 : 11.057
GeekforGeeks64___
GeekforGeeks73
GeekforGeeks6
Sum of list : 15 Square of int : 25 String : Hello! GeeksforGeeks.7
equation 2 : -3 equation 1 : 2 equation 3 : 11.081
equation 2 : -3 equation 1 : 2 equation 3 : 11.082
First number = 1000 Second number = 2000 Addition of two numbers = 300027
GeekforGeeks65
equation 2 : -3 equation 1 : 2 equation 3 : 11.086
GeekforGeeks6
equation 2 : -3 equation 1 : 2 equation 3 : 11.088
‘
GeekforGeeks3
equation 2 : -3 equation 1 : 2 equation 3 : 11.098
GeekforGeeks6
class
00GeekforGeeks3
First number = 1000 Second number = 2000 Addition of two numbers = 30004
First number = 1000 Second number = 2000 Addition of two numbers = 30005
equation 2 : -3 equation 1 : 2 equation 3 : 11.028
class
05
GeekforGeeks83
GeekforGeeks62
First number = 1000 Second number = 2000 Addition of two numbers = 300067
GeekforGeeks6
GeekforGeeks6
First number = 1000 Second number = 2000 Addition of two numbers = 300027
GeekforGeeks65
GeekforGeeks3
equation 2 : -3 equation 1 : 2 equation 3 : 11.098
GeekforGeeks6
class
17GeekforGeeks3
First number = 1000 Second number = 2000 Addition of two numbers = 30004
First number = 1000 Second number = 2000 Addition of two numbers = 30005
equation 2 : -3 equation 1 : 2 equation 3 : 11.032
class
05
First number = 1000 Second number = 2000 Addition of two numbers = 300093
GeekforGeeks65
GeekforGeeks3
equation 2 : -3 equation 1 : 2 equation 3 : 11.098
GeekforGeeks6
class
29GeekforGeeks3
First number = 1000 Second number = 2000 Addition of two numbers = 30004
First number = 1000 Second number = 2000 Addition of two numbers = 30005
equation 2 : -3 equation 1 : 2 equation 3 : 11.036
class
05
GeekforGeeks73
GeekforGeeks:
0GeekforGeeks6
GeekforGeeks64
Đầu ra
equation 2 : -3 equation 1 : 2 equation 3 : 11.0
Trong ví dụ trên, trường hợp của đối tượng không được tạo ban đầu. Các phương pháp lớp để đánh giá các biểu thức khác nhau đã được xác định với bộ trang trí @ClassMethod. Bây giờ chúng có thể được gọi với tên lớp và đối tượng được tạo trong phương thức lớp đó sau khi đánh giá biểu thức. Biến trường hợp giữ các câu trả lời khác nhau cho một số lượng tham số khác nhau được truyền.@classmethod decorator. Now they can be called with the class name and the object is created in that class method after evaluating the expression. The instance variable holds different answers for a different number of parameters passed.