Các phương thức của lớp là các phương thức được gọi trên một lớp chứ không phải trên một thể hiện. Chúng thường được sử dụng như một phần của siêu mô hình đối tượng. Tôi. e, đối với mỗi lớp, một thể hiện của đối tượng lớp được xác định trong siêu mô hình được tạo. Các giao thức siêu mô hình cho phép tạo và xóa các lớp. Theo nghĩa này, chúng cung cấp chức năng giống như hàm tạo và hàm hủy được mô tả ở trên. Nhưng trong một số ngôn ngữ chẳng hạn như Common Lisp Object System [CLOS], siêu mô hình cho phép nhà phát triển dynami
Tôi có 2 lớp
class impute[model]:
def __init__[self]:
super[].__init__[]
for i in ['MasVnrType', 'BsmtQual', 'BsmtFinType1', 'GarageType', 'GarageFinish']:
self.mode_impute[self.x, i]
def mode_impute[self]:
mode_val = self.df6[self.var].value_counts[].index[0]
self.df6[self.var].fillna[mode_val, inplace = True]
m = impute[]
3 và class impute[model]:
def __init__[self]:
super[].__init__[]
for i in ['MasVnrType', 'BsmtQual', 'BsmtFinType1', 'GarageType', 'GarageFinish']:
self.mode_impute[self.x, i]
def mode_impute[self]:
mode_val = self.df6[self.var].value_counts[].index[0]
self.df6[self.var].fillna[mode_val, inplace = True]
m = impute[]
4. Tôi đang định nghĩa một hàm class impute[model]:
def __init__[self]:
super[].__init__[]
for i in ['MasVnrType', 'BsmtQual', 'BsmtFinType1', 'GarageType', 'GarageFinish']:
self.mode_impute[self.x, i]
def mode_impute[self]:
mode_val = self.df6[self.var].value_counts[].index[0]
self.df6[self.var].fillna[mode_val, inplace = True]
m = impute[]
5 bên trong class impute[model]:
def __init__[self]:
super[].__init__[]
for i in ['MasVnrType', 'BsmtQual', 'BsmtFinType1', 'GarageType', 'GarageFinish']:
self.mode_impute[self.x, i]
def mode_impute[self]:
mode_val = self.df6[self.var].value_counts[].index[0]
self.df6[self.var].fillna[mode_val, inplace = True]
m = impute[]
4. Bây giờ tôi muốn gọi class impute[model]:
def __init__[self]:
super[].__init__[]
for i in ['MasVnrType', 'BsmtQual', 'BsmtFinType1', 'GarageType', 'GarageFinish']:
self.mode_impute[self.x, i]
def mode_impute[self]:
mode_val = self.df6[self.var].value_counts[].index[0]
self.df6[self.var].fillna[mode_val, inplace = True]
m = impute[]
5 bên trong class impute[model]:
def __init__[self]:
super[].__init__[]
for i in ['MasVnrType', 'BsmtQual', 'BsmtFinType1', 'GarageType', 'GarageFinish']:
self.mode_impute[self.x, i]
def mode_impute[self]:
mode_val = self.df6[self.var].value_counts[].index[0]
self.df6[self.var].fillna[mode_val, inplace = True]
m = impute[]
4. Làm thế nào tôi có thể gọi nó? class impute[model]:
def __init__[self]:
super[].__init__[]
pass
def mode_impute[self]:
mode_val = self.df6[self.var].value_counts[].index[0]
self.df6[self.var].fillna[mode_val, inplace = True]
for i in ['MasVnrType', 'BsmtQual', 'BsmtFinType1', 'GarageType', 'GarageFinish']:
self.mode_impute[self.x, i]
Đoạn mã trên cho tôi lỗi
class impute[model]:
def __init__[self]:
super[].__init__[]
for i in ['MasVnrType', 'BsmtQual', 'BsmtFinType1', 'GarageType', 'GarageFinish']:
self.mode_impute[self.x, i]
def mode_impute[self]:
mode_val = self.df6[self.var].value_counts[].index[0]
self.df6[self.var].fillna[mode_val, inplace = True]
m = impute[]
9CHỈNH SỬA 1
Tôi đã áp dụng các thay đổi như đề xuất trong nhận xét
class impute[model]:
def __init__[self]:
super[].__init__[]
for i in ['MasVnrType', 'BsmtQual', 'BsmtFinType1', 'GarageType', 'GarageFinish']:
self.mode_impute[self.x, i]
def mode_impute[self]:
mode_val = self.df6[self.var].value_counts[].index[0]
self.df6[self.var].fillna[mode_val, inplace = True]
m = impute[]
Dòng cuối cùng nơi tôi tạo một thể hiện của lớp cho tôi lỗi
class impute[model]:
def __init__[self]:
super[].__init__[]
for i in ['MasVnrType', 'BsmtQual', 'BsmtFinType1', 'GarageType', 'GarageFinish']:
self.mode_impute[self.x, i]
def mode_impute[self]:
mode_val = self.df6[self.var].value_counts[].index[0]
self.df6[self.var].fillna[mode_val, inplace = True]
m = impute[]
0Tái bút. Tôi mới bắt đầu học OOP cho python nên vui lòng giải thích câu trả lời của bạn một cách đơn giản và dễ hiểu. Cảm ơn bạn
Trong hình bên dưới. Lệnh gọi hàm được thực hiện từ Hàm chính đến Hàm 1, Bây giờ trạng thái của Hàm chính được lưu trữ trong Ngăn xếp và việc thực thi Hàm chính được tiếp tục khi Hàm 1 trả về. Hàm 1 gọi Hàm 2 bây giờ Trạng thái của Hàm 1 được lưu trữ trong ngăn xếp và việc thực thi Hàm 1 sẽ được tiếp tục khi Hàm 2 trả về.
Xem xét ví dụ dưới đây về lệnh gọi hàm. Hàm SumOfSquares gọi Hàm Square trả về bình phương của số.
Python3
Function2 : Hello Function1 : World0
Function2 : Hello Function1 : World1
Function2 : Hello Function1 : World2
Function2 : Hello Function1 : World3
Function2 : Hello Function1 : World4
Function2 : Hello Function1 : World5
Function2 : Hello Function1 : World4
Function2 : Hello Function1 : World0
Function2 : Hello Function1 : World4
Function2 : Hello Function1 : World2
Function2 : Hello Function1 : World3______34
Function2 : Hello Function1 : World5
Function2 : Hello Function1 : World2
Function2 : Hello Function1 : World7
Function2 : Hello Function1 : World4
Function2 : Hello Function1 : World9
Function2 : Hello Function1 : World4
Function2 : Hello Function1 : World1
Function2 : Hello Function1 : World4
Function2 : Hello Function1 : World3
Function2 : Hello Function1 : World4
Function2 : Hello Function1 : World5
Function2 : Hello Function1 : World4
Function2 : Hello Function1 : World7
Function2 : Hello Function1 : World8
Function2 : Hello Function1 : World9
Function2 : Hello Function1 : World00
Function2 : Hello Function1 : World01
Function2 : Hello Function1 : World02
Function2 : Hello Function1 : World03
Function2 : Hello Function1 : World02
Function2 : Hello Function1 : World05
Function2 : Hello Function1 : World4
Function2 : Hello Function1 : World07
Function2 : Hello Function1 : World02
Function2 : Hello Function1 : World09
Function2 : Hello Function1 : World02
Function2 : Hello Function1 : World3
Function2 : Hello Function1 : World12______34
Function2 : Hello Function1 : World05
Function2 : Hello Function1 : World4
Function2 : Hello Function1 : World2
Function2 : Hello Function1 : World3
Function2 : Hello Function1 : World18
Function2 : Hello Function1 : World19
Function2 : Hello Function1 : World4
Function2 : Hello Function1 : World21
Function2 : Hello Function1 : World22
Function2 : Hello Function1 : World23
Function2 : Hello Function1 : World24
Function2 : Hello Function1 : World23
Function2 : Hello Function1 : World26
Function2 : Hello Function1 : World23
Function2 : Hello Function1 : World28
Function2 : Hello Function1 : World23
Function2 : Hello Function1 : World30
Function2 : Hello Function1 : World23
Function2 : Hello Function1 : World32
Function2 : Hello Function1 : World23
Function2 : Hello Function1 : World34
Function2 : Hello Function1 : World23
Function2 : Hello Function1 : World36
Function2 : Hello Function1 : World23
Function2 : Hello Function1 : World38
Function2 : Hello Function1 : World23
Function2 : Hello Function1 : World40
Function2 : Hello Function1 : World41