Bản tóm tắt. trong hướng dẫn này, bạn sẽ học cách sử dụng phương thức
0 dunder của Python và sự khác biệt giữa phương thứcCode language: Python [python]
person = Person['John', 'Doe', 25] print[repr[person]]
0 vàCode language: Python [python]
person = Person['John', 'Doe', 25] print[repr[person]]
2Code language: Python [python]
person = Person['John', 'Doe', 25] print[repr[person]]
Giới thiệu về phương pháp ma thuật Python person = Person['John', 'Doe', 25]
print[repr[person]]
Code language: Python [python]
0
person = Person['John', 'Doe', 25]
print[repr[person]]
Phương thức dunder của
0 định nghĩa hành vi khi bạn truyền một thể hiện của một lớp choCode language: Python [python]
person = Person['John', 'Doe', 25] print[repr[person]]
5Code language: Python [python]
person = Person['John', 'Doe', 25] print[repr[person]]
Phương thức
0 trả về biểu diễn chuỗi của một đối tượng. Thông thường,Code language: Python [python]
person = Person['John', 'Doe', 25] print[repr[person]]
7 trả về một chuỗi có thể được thực thi và mang lại giá trị giống như đối tượngCode language: Python [python]
person = Person['John', 'Doe', 25] print[repr[person]]
Nói cách khác, nếu bạn chuyển chuỗi trả về của phương thức
8 cho hàmCode language: Python [python]
person = Person['John', 'Doe', 25] print[repr[person]]
9, bạn sẽ nhận được giá trị giống như giá trị của phương thứcCode language: Python [python]
person = Person['John', 'Doe', 25] print[repr[person]]
00. Hãy xem một ví dụCode language: Python [python]
person = Person['John', 'Doe', 25] print[repr[person]]
Đầu tiên, định nghĩa lớp
01 với ba thuộc tính thể hiện
person = Person['John', 'Doe', 25] print[repr[person]]
Code language: Python [python]
02,Code language: Python [python]
person = Person['John', 'Doe', 25] print[repr[person]]
03 vàCode language: Python [python]
person = Person['John', 'Doe', 25] print[repr[person]]
04Code language: Python [python]
person = Person['John', 'Doe', 25] print[repr[person]]
5Code language: Python [python]
person = Person['John', 'Doe', 25] print[repr[person]]
Thứ hai, tạo một thể hiện mới của lớp
01 và hiển thị biểu diễn chuỗi của nóCode language: Python [python]
person = Person['John', 'Doe', 25] print[repr[person]]
Code language: Python [python]
person = Person['John', 'Doe', 25] print[repr[person]]
đầu ra
0Code language: Python [python]
person = Person['John', 'Doe', 25] print[repr[person]]
Theo mặc định, đầu ra chứa địa chỉ bộ nhớ của đối tượng
06. Để tùy chỉnh biểu diễn chuỗi của đối tượng, bạn có thể triển khai phương thứcCode language: Python [python]
person = Person['John', 'Doe', 25] print[repr[person]]
0 như thế nàyCode language: Python [python]
person = Person['John', 'Doe', 25] print[repr[person]]
1Code language: Python [python]
person = Person['John', 'Doe', 25] print[repr[person]]
Khi bạn truyền một thể hiện của lớp
01 cho lớpCode language: Python [python]
person = Person['John', 'Doe', 25] print[repr[person]]
5, Python sẽ tự động gọi phương thức
person = Person['John', 'Doe', 25] print[repr[person]]
Code language: Python [python]
0. Ví dụCode language: Python [python]
person = Person['John', 'Doe', 25] print[repr[person]]
5Code language: Python [python]
person = Person['John', 'Doe', 25] print[repr[person]]
đầu ra
6Code language: Python [python]
person = Person['John', 'Doe', 25] print[repr[person]]
Nếu bạn thực thi chuỗi trả về
11, nó sẽ trả về đối tượngCode language: Python [python]
person = Person['John', 'Doe', 25] print[repr[person]]
06Code language: Python [python]
person = Person['John', 'Doe', 25] print[repr[person]]
Khi một lớp không triển khai phương thức
13 và bạn chuyển một thể hiện của lớp đó choCode language: Python [python]
person = Person['John', 'Doe', 25] print[repr[person]]
14, Python trả về kết quả của phương thứcCode language: Python [python]
person = Person['John', 'Doe', 25] print[repr[person]]
0 vì bên trong phương thứcCode language: Python [python]
person = Person['John', 'Doe', 25] print[repr[person]]
2 gọi phương thứcCode language: Python [python]
person = Person['John', 'Doe', 25] print[repr[person]]
0Code language: Python [python]
person = Person['John', 'Doe', 25] print[repr[person]]
Ví dụ
4
person = Person['John', 'Doe', 25] print[repr[person]]
Code language: Python [python]
đầu ra
6Code language: Python [python]
person = Person['John', 'Doe', 25] print[repr[person]]
Nếu một lớp triển khai phương thức
2, Python sẽ gọi phương thứcCode language: Python [python]
person = Person['John', 'Doe', 25] print[repr[person]]
2 khi bạn truyền một thể hiện của lớp đó choCode language: Python [python]
person = Person['John', 'Doe', 25] print[repr[person]]
14. Ví dụCode language: Python [python]
person = Person['John', 'Doe', 25] print[repr[person]]
9Code language: Python [python]
person = Person['John', 'Doe', 25] print[repr[person]]
đầu ra
0Code language: Python [python]
person = Person['John', 'Doe', 25] print[repr[person]]
person = Person['John', 'Doe', 25]
print[repr[person]]
Code language: Python [python]
2 đấu với person = Person['John', 'Doe', 25]
print[repr[person]]
Code language: Python [python]
0
person = Person['John', 'Doe', 25]
print[repr[person]]
person = Person['John', 'Doe', 25]
print[repr[person]]
Sự khác biệt chính giữa phương pháp
2 vàCode language: Python [python]
person = Person['John', 'Doe', 25] print[repr[person]]
0 là đối tượng dự địnhCode language: Python [python]
person = Person['John', 'Doe', 25] print[repr[person]]
Phương thức
2 trả về một biểu diễn chuỗi của một đối tượng mà con người có thể đọc được trong khi phương thứcCode language: Python [python]
person = Person['John', 'Doe', 25] print[repr[person]]
0 trả về một biểu diễn chuỗi của một đối tượng mà máy có thể đọc đượcCode language: Python [python]
person = Person['John', 'Doe', 25] print[repr[person]]