Repr và str trong Python là gì?

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

person = Person['John', 'Doe', 25] print[repr[person]]

Code language: Python [python]
0 dunder của Python và sự khác biệt giữa phương thức

person = Person['John', 'Doe', 25] print[repr[person]]

Code language: Python [python]
0 và

person = Person['John', 'Doe', 25] print[repr[person]]

Code language: Python [python]
2

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

Phương thức dunder của

person = Person['John', 'Doe', 25] print[repr[person]]

Code language: Python [python]
0 định nghĩa hành vi khi bạn truyền một thể hiện của một lớp cho

person = Person['John', 'Doe', 25] print[repr[person]]

Code language: Python [python]
5

Phương thức

person = Person['John', 'Doe', 25] print[repr[person]]

Code language: Python [python]
0 trả về biểu diễn chuỗi của một đối tượng. Thông thường,

person = Person['John', 'Doe', 25] print[repr[person]]

Code language: Python [python]
7 trả về một chuỗi có thể được thực thi và mang lại giá trị giống như đối tượng

Nói cách khác, nếu bạn chuyển chuỗi trả về của phương thức

person = Person['John', 'Doe', 25] print[repr[person]]

Code language: Python [python]
8 cho hàm

person = Person['John', 'Doe', 25] print[repr[person]]

Code language: Python [python]
9, bạn sẽ nhận được giá trị giống như giá trị của phương thức

person = Person['John', 'Doe', 25] print[repr[person]]

Code language: Python [python]
00. Hãy xem một ví dụ

Đầu tiên, định nghĩa lớp

person = Person['John', 'Doe', 25] print[repr[person]]

Code language: Python [python]
01 với ba thuộc tính thể hiện

person = Person['John', 'Doe', 25] print[repr[person]]

Code language: Python [python]
02,

person = Person['John', 'Doe', 25] print[repr[person]]

Code language: Python [python]
03 và

person = Person['John', 'Doe', 25] print[repr[person]]

Code language: Python [python]
04

person = Person['John', 'Doe', 25] print[repr[person]]

Code language: Python [python]
5

Thứ hai, tạo một thể hiện mới của lớp

person = Person['John', 'Doe', 25] print[repr[person]]

Code language: Python [python]
01 và hiển thị biểu diễn chuỗi của nó

person = Person['John', 'Doe', 25] print[repr[person]]

Code language: Python [python]

đầu ra

person = Person['John', 'Doe', 25] print[repr[person]]

Code language: Python [python]
0

Theo mặc định, đầu ra chứa địa chỉ bộ nhớ của đối tượng

person = Person['John', 'Doe', 25] print[repr[person]]

Code language: Python [python]
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ức

person = Person['John', 'Doe', 25] print[repr[person]]

Code language: Python [python]
0 như thế này

person = Person['John', 'Doe', 25] print[repr[person]]

Code language: Python [python]
1

Khi bạn truyền một thể hiện của lớp

person = Person['John', 'Doe', 25] print[repr[person]]

Code language: Python [python]
01 cho lớp

person = Person['John', 'Doe', 25] print[repr[person]]

Code language: Python [python]
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ụ

person = Person['John', 'Doe', 25] print[repr[person]]

Code language: Python [python]
5

đầu ra

person = Person['John', 'Doe', 25] print[repr[person]]

Code language: Python [python]
6

Nếu bạn thực thi chuỗi trả về

person = Person['John', 'Doe', 25] print[repr[person]]

Code language: Python [python]
11, nó sẽ trả về đối tượng

person = Person['John', 'Doe', 25] print[repr[person]]

Code language: Python [python]
06

Khi một lớp không triển khai phương thức

person = Person['John', 'Doe', 25] print[repr[person]]

Code language: Python [python]
13 và bạn chuyển một thể hiện của lớp đó cho

person = Person['John', 'Doe', 25] print[repr[person]]

Code language: Python [python]
14, Python trả về kết quả của phương thức

person = Person['John', 'Doe', 25] print[repr[person]]

Code language: Python [python]
0 vì bên trong phương thức

person = Person['John', 'Doe', 25] print[repr[person]]

Code language: Python [python]
2 gọi phương thức

person = Person['John', 'Doe', 25] print[repr[person]]

Code language: Python [python]
0

Ví dụ

person = Person['John', 'Doe', 25] print[repr[person]]

Code language: Python [python]
4

đầu ra

person = Person['John', 'Doe', 25] print[repr[person]]

Code language: Python [python]
6

Nếu một lớp triển khai phương thức

person = Person['John', 'Doe', 25] print[repr[person]]

Code language: Python [python]
2, Python sẽ gọi phương thức

person = Person['John', 'Doe', 25] print[repr[person]]

Code language: Python [python]
2 khi bạn truyền một thể hiện của lớp đó cho

person = Person['John', 'Doe', 25] print[repr[person]]

Code language: Python [python]
14. Ví dụ

person = Person['John', 'Doe', 25] print[repr[person]]

Code language: Python [python]
9

đầu ra

person = Person['John', 'Doe', 25] print[repr[person]]

Code language: Python [python]
0

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

Sự khác biệt chính giữa phương pháp

person = Person['John', 'Doe', 25] print[repr[person]]

Code language: Python [python]
2 và

person = Person['John', 'Doe', 25] print[repr[person]]

Code language: Python [python]
0 là đối tượng dự định

Phương thức

person = Person['John', 'Doe', 25] print[repr[person]]

Code language: Python [python]
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ức

person = Person['John', 'Doe', 25] print[repr[person]]

Code language: Python [python]
0 trả về một biểu diễn chuỗi của một đối tượng mà máy có thể đọc được

str và repr trong Python là gì?

repr[] tính toán biểu diễn chuỗi “chính thức” của một đối tượng [một biểu diễn có tất cả thông tin về đối tượng] và str[] được sử dụng để tính toán biểu diễn chuỗi “không chính thức” của một đối tượng [một biểu diễn hữu ích cho việc in

Một repr trong Python là gì?

Định nghĩa. Hàm tích hợp repr[] trong Python trả về biểu diễn có thể in được của đối tượng đã chỉ định dưới dạng một chuỗi . Hàm repr[] trong Python trả về một biểu diễn có thể in được của đối tượng bằng cách chuyển đổi đối tượng đó thành một chuỗi.

STR trong Python là gì?

Python có một lớp chuỗi dựng sẵn tên là "str" ​​với nhiều tính năng tiện dụng [có một mô-đun cũ hơn tên là "string" . Chuỗi ký tự có thể được đặt trong dấu nháy kép hoặc dấu nháy đơn, mặc dù dấu nháy đơn được sử dụng phổ biến hơn.

Mục đích của việc định nghĩa các hàm __ str __ và __ repr __ trong một lớp là hai hàm khác nhau như thế nào?

__str__ được sử dụng để hiển thị một chuỗi đại diện cho đối tượng của bạn để người khác dễ dàng đọc được. __repr__ được sử dụng để hiển thị biểu diễn chuỗi của đối tượng. Lưu câu trả lời này

Chủ Đề