Cải thiện bài viết
Lưu bài viết
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luận
Tự đại diện cho trường hợp của lớp. Bằng cách sử dụng bản thân của người Viking & NBSP;, chúng ta có thể truy cập các thuộc tính và phương thức của lớp trong Python. Nó liên kết các thuộc tính với các đối số đã cho.
Lý do bạn cần sử dụng bản thân. là bởi vì Python không sử dụng @ Cú pháp để chỉ các thuộc tính thể hiện. Python quyết định thực hiện các phương thức theo cách làm cho thể hiện phương thức được truyền tự động, nhưng không nhận được tự động: tham số đầu tiên của các phương thức là phiên bản phương thức được gọi.
Python3
Theo cách rõ ràng hơn, bạn có thể nói rằng bản thân có đặc điểm sau-
Bản thân luôn chỉ vào đối tượng hiện tại.
Model is audi a4 color is blue Model is ferrari 488 color is green0
Model is audi a4 color is blue Model is ferrari 488 color is green1
Model is audi a4 color is blue Model is ferrari 488 color is green2
Model is audi a4 color is blue Model is ferrari 488 color is green3
Model is audi a4 color is blue Model is ferrari 488 color is green4
Model is audi a4 color is blue Model is ferrari 488 color is green5
Model is audi a4 color is blue Model is ferrari 488 color is green2
self
Model is audi a4 color is blue Model is ferrari 488 color is green8
class
check:
Model is audi a4 color is blue Model is ferrari 488 color is green1
Model is audi a4 color is blue Model is ferrari 488 color is green2
we have used another parameter name in place of self4
Model is audi a4 color is blue Model is ferrari 488 color is green4
Model is audi a4 color is blue Model is ferrari 488 color is green5
we have used another parameter name in place of self7
def
__init__[
self
Address of self = 140124194801032 Address of class object = 140124194801032
Model is audi a4 color is blue Model is ferrari 488 color is green9
we have used another parameter name in place of self0
we have used another parameter name in place of self1
Python3
Đầu ra
Một ví dụ khác về việc sử dụng bản thân:
class
we have used another parameter name in place of self9
def
__init__[
self
class
4
Model is audi a4 color is blue Model is ferrari 488 color is green0
self
class
7we have used another parameter name in place of self0
class
9Model is audi a4 color is blue Model is ferrari 488 color is green0
Model is audi a4 color is blue Model is ferrari 488 color is green1
Model is audi a4 color is blue Model is ferrari 488 color is green2
3
4self
6Model is audi a4 color is blue Model is ferrari 488 color is green0
Model is audi a4 color is blue Model is ferrari 488 color is green1
Model is audi a4 color is blue Model is ferrari 488 color is green2
def
0
4self
def
3Model is audi a4 color is blue Model is ferrari 488 color is green0
self
check:
2we have used another parameter name in place of self0
check:
4
def
check:
7self
__
__init__[
8
__init__[
9
Model is audi a4 color is blue Model is ferrari 488 color is green1
Model is audi a4 color is blue Model is ferrari 488 color is green2
self
2self
3Model is audi a4 color is blue Model is ferrari 488 color is green1
Model is audi a4 color is blue Model is ferrari 488 color is green2
self
6self
7
def
__init__[
self
Model is audi a4 color is blue Model is ferrari 488 color is green
Model is audi a4 color is blue Model is ferrari 488 color is green9we have used another parameter name in place of self0we have used another parameter name in place of self1
Đầu ra
Python3
Theo cách rõ ràng hơn, bạn có thể nói rằng bản thân có đặc điểm sau-
Bản thân luôn chỉ vào đối tượng hiện tại.
Model is audi a4 color is blue Model is ferrari 488 color is green0
Model is audi a4 color is blue Model is ferrari 488 color is green1
Model is audi a4 color is blue Model is ferrari 488 color is green2
]:
6__init__[
0class
check:
Model is audi a4 color is blue Model is ferrari 488 color is green1
Model is audi a4 color is blue Model is ferrari 488 color is green2
Model is audi a4 color is blue Model is ferrari 488 color is green03
__init__[
0
def
__init__[
self
Model is audi a4 color is blue Model is ferrari 488 color is green9
we have used another parameter name in place of self0
we have used another parameter name in place of self1
Đầu ra
Một ví dụ khác về việc sử dụng bản thân:
class
we have used another parameter name in place of self9
def
__init__[
self
class
4
Model is audi a4
color is blue
Model is ferrari 488
color is green
0self
class
7we have used another parameter name in place of self
0 class
9
Model is audi a4 color is blue Model is ferrari 488 color is green0
self
check:
2we have used another parameter name in place of self0
check:
4
def
check:
7self
__
Model is audi a4 color is blue Model is ferrari 488 color is green0
Model is audi a4 color is blue Model is ferrari 488 color is green1
Model is audi a4 color is blue Model is ferrari 488 color is green2
Model is audi a4 color is blue Model is ferrari 488 color is green31
Model is audi a4 color is blue Model is ferrari 488 color is green0
Model is audi a4 color is blue Model is ferrari 488 color is green33
Model is audi a4 color is blue Model is ferrari 488 color is green34
def
4
we have used another parameter name in place of self0
def
6def
7
4__6
def
__init__[
self
we have used another parameter name in place of self