ID đối tượng của các chuỗi có thể khác nhau trên các máy khác nhau. ID đối tượng của str1, str2 và str3 giống nhau nên kết quả là True trong mọi trường hợp. Sau khi id đối tượng của str1 bị thay đổi, kết quả của str1 và str2 sẽ là false. Ngay cả sau khi tạo str4 với cùng nội dung như trong str1 mới, câu trả lời sẽ sai vì ID đối tượng của chúng khác nhau
Ngược lại sẽ xảy ra với is not
Phương pháp 3. Tạo một hàm do người dùng định nghĩa
Bằng cách sử dụng các toán tử quan hệ, chúng ta chỉ có thể so sánh các chuỗi theo unicodes của chúng. Để so sánh hai chuỗi theo một số tham số khác, chúng ta có thể tạo các hàm do người dùng định nghĩa
Trong đoạn mã sau, hàm do người dùng xác định của chúng tôi sẽ so sánh các chuỗi dựa trên số chữ số
Python3
ID of str1 = 0x7f6037051570 ID of str2 = 0x7f6037051570 ID of str3 = 0x7f6037051570 True True True ID of changed str1 = 0x7f60356137d8 ID of str4 = 0x7f60356137a0 False04
ID of str1 = 0x7f6037051570 ID of str2 = 0x7f6037051570 ID of str3 = 0x7f6037051570 True True True ID of changed str1 = 0x7f60356137d8 ID of str4 = 0x7f60356137a0 False05
ID of str1 = 0x7f6037051570 ID of str2 = 0x7f6037051570 ID of str3 = 0x7f6037051570 True True True ID of changed str1 = 0x7f60356137d8 ID of str4 = 0x7f60356137a0 False06
ID of str1 = 0x7f6037051570 ID of str2 = 0x7f6037051570 ID of str3 = 0x7f6037051570 True True True ID of changed str1 = 0x7f60356137d8 ID of str4 = 0x7f60356137a0 False07
ID of str1 = 0x7f6037051570 ID of str2 = 0x7f6037051570 ID of str3 = 0x7f6037051570 True True True ID of changed str1 = 0x7f60356137d8 ID of str4 = 0x7f60356137a0 False08
ID of str1 = 0x7f6037051570 ID of str2 = 0x7f6037051570 ID of str3 = 0x7f6037051570 True True True ID of changed str1 = 0x7f60356137d8 ID of str4 = 0x7f60356137a0 False09
ID of str1 = 0x7f6037051570 ID of str2 = 0x7f6037051570 ID of str3 = 0x7f6037051570 True True True ID of changed str1 = 0x7f60356137d8 ID of str4 = 0x7f60356137a0 False6
ID of str1 = 0x7f6037051570 ID of str2 = 0x7f6037051570 ID of str3 = 0x7f6037051570 True True True ID of changed str1 = 0x7f60356137d8 ID of str4 = 0x7f60356137a0 False11
ID of str1 = 0x7f6037051570 ID of str2 = 0x7f6037051570 ID of str3 = 0x7f6037051570 True True True ID of changed str1 = 0x7f60356137d8 ID of str4 = 0x7f60356137a0 False08
ID of str1 = 0x7f6037051570 ID of str2 = 0x7f6037051570 ID of str3 = 0x7f6037051570 True True True ID of changed str1 = 0x7f60356137d8 ID of str4 = 0x7f60356137a0 False13____26
ID of str1 = 0x7f6037051570 ID of str2 = 0x7f6037051570 ID of str3 = 0x7f6037051570 True True True ID of changed str1 = 0x7f60356137d8 ID of str4 = 0x7f60356137a0 False11
ID of str1 = 0x7f6037051570 ID of str2 = 0x7f6037051570 ID of str3 = 0x7f6037051570 True True True ID of changed str1 = 0x7f60356137d8 ID of str4 = 0x7f60356137a0 False16
ID of str1 = 0x7f6037051570 ID of str2 = 0x7f6037051570 ID of str3 = 0x7f6037051570 True True True ID of changed str1 = 0x7f60356137d8 ID of str4 = 0x7f60356137a0 False08
ID of str1 = 0x7f6037051570 ID of str2 = 0x7f6037051570 ID of str3 = 0x7f6037051570 True True True ID of changed str1 = 0x7f60356137d8 ID of str4 = 0x7f60356137a0 False18
ID of str1 = 0x7f6037051570 ID of str2 = 0x7f6037051570 ID of str3 = 0x7f6037051570 True True True ID of changed str1 = 0x7f60356137d8 ID of str4 = 0x7f60356137a0 False19
ID of str1 = 0x7f6037051570 ID of str2 = 0x7f6037051570 ID of str3 = 0x7f6037051570 True True True ID of changed str1 = 0x7f60356137d8 ID of str4 = 0x7f60356137a0 False20
ID of str1 = 0x7f6037051570 ID of str2 = 0x7f6037051570 ID of str3 = 0x7f6037051570 True True True ID of changed str1 = 0x7f60356137d8 ID of str4 = 0x7f60356137a0 False21
ID of str1 = 0x7f6037051570 ID of str2 = 0x7f6037051570 ID of str3 = 0x7f6037051570 True True True ID of changed str1 = 0x7f60356137d8 ID of str4 = 0x7f60356137a0 False8
ID of str1 = 0x7f6037051570 ID of str2 = 0x7f6037051570 ID of str3 = 0x7f6037051570 True True True ID of changed str1 = 0x7f60356137d8 ID of str4 = 0x7f60356137a0 False23
ID of str1 = 0x7f6037051570 ID of str2 = 0x7f6037051570 ID of str3 = 0x7f6037051570 True True True ID of changed str1 = 0x7f60356137d8 ID of str4 = 0x7f60356137a0 False24
ID of str1 = 0x7f6037051570 ID of str2 = 0x7f6037051570 ID of str3 = 0x7f6037051570 True True True ID of changed str1 = 0x7f60356137d8 ID of str4 = 0x7f60356137a0 False25
ID of str1 = 0x7f6037051570 ID of str2 = 0x7f6037051570 ID of str3 = 0x7f6037051570 True True True ID of changed str1 = 0x7f60356137d8 ID of str4 = 0x7f60356137a0 False26
ID of str1 = 0x7f6037051570 ID of str2 = 0x7f6037051570 ID of str3 = 0x7f6037051570 True True True ID of changed str1 = 0x7f60356137d8 ID of str4 = 0x7f60356137a0 False51
ID of str1 = 0x7f6037051570 ID of str2 = 0x7f6037051570 ID of str3 = 0x7f6037051570 True True True ID of changed str1 = 0x7f60356137d8 ID of str4 = 0x7f60356137a0 False6
ID of str1 = 0x7f6037051570 ID of str2 = 0x7f6037051570 ID of str3 = 0x7f6037051570 True True True ID of changed str1 = 0x7f60356137d8 ID of str4 = 0x7f60356137a0 False29
ID of str1 = 0x7f6037051570 ID of str2 = 0x7f6037051570 ID of str3 = 0x7f6037051570 True True True ID of changed str1 = 0x7f60356137d8 ID of str4 = 0x7f60356137a0 False30
ID of str1 = 0x7f6037051570 ID of str2 = 0x7f6037051570 ID of str3 = 0x7f6037051570 True True True ID of changed str1 = 0x7f60356137d8 ID of str4 = 0x7f60356137a0 False55
ID of str1 = 0x7f6037051570 ID of str2 = 0x7f6037051570 ID of str3 = 0x7f6037051570 True True True ID of changed str1 = 0x7f60356137d8 ID of str4 = 0x7f60356137a0 False6
ID of str1 = 0x7f6037051570 ID of str2 = 0x7f6037051570 ID of str3 = 0x7f6037051570 True True True ID of changed str1 = 0x7f60356137d8 ID of str4 = 0x7f60356137a0 False33
ID of str1 = 0x7f6037051570 ID of str2 = 0x7f6037051570 ID of str3 = 0x7f6037051570 True True True ID of changed str1 = 0x7f60356137d8 ID of str4 = 0x7f60356137a0 False34