Đối với một đối tượng tùy ý, toán tử
class Phone:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
pn = Phone[873555333]
1 sẽ chỉ trả về true nếu hai đối tượng là cùng một đối tượng [nghĩa là nếu chúng tham khảo cùng một địa chỉ trong bộ nhớ].Để có được nhiều hành vi 'bespoke' hơn, bạn sẽ muốn ghi đè các toán tử so sánh phong phú, trong trường hợp này cụ thể là
class Phone:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
pn = Phone[873555333]
2. Hãy thử thêm cái này vào lớp của bạn:def __eq__[self, other]:
if self.param == other.param \
and self.param_2 == other.param_2 \
and self.param_3 == other.param_3:
return True
else:
return False
[Việc so sánh tất cả các thông số có thể được gọn gàng ở đây, nhưng tôi đã để chúng rõ ràng].
Lưu ý rằng nếu các tham số là các đối tượng bạn đã xác định, các đối tượng đó sẽ phải xác định
class Phone:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
pn = Phone[873555333]
2 theo cách tương tự để điều này hoạt động.Một điểm khác cần lưu ý là nếu bạn cố gắng so sánh một đối tượng Foobar với một loại đối tượng khác theo cách tôi đã làm ở trên, Python sẽ cố gắng truy cập các thuộc tính param, param_2 và param_3 của loại đối tượng khác sẽ ném một Thuộc tính. Có lẽ bạn sẽ muốn kiểm tra đối tượng bạn đang so sánh là một ví dụ của Foobar với isinstance [khác, foobar] trước tiên. Điều này không được thực hiện theo mặc định vì có thể có những tình huống mà bạn muốn trả về true để so sánh giữa các loại khác nhau.
Xem câu trả lời của AJ cho một cách gọn gàng hơn để chỉ đơn giản là so sánh tất cả các tham số cũng không nên ném lỗi thuộc tính.
Để biết thêm thông tin về so sánh phong phú, hãy xem các tài liệu Python.
Trong bài tập trước, bạn đã xác định một lớp
class Phone:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
pn = Phone[873555333]
4 với thuộc tính
class Phone:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
pn = Phone[873555333]
5 được sử dụng để so sánh. Nhưng nếu bạn so sánh một đối tượng
class Phone:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
pn = Phone[873555333]
4 với một đối tượng của một lớp khác cũng có thuộc tính
class Phone:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
pn = Phone[873555333]
5, bạn có thể kết thúc với kết quả không mong muốn.Ví dụ: xem xét hai lớp
|
|
Chạy
class Phone:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
pn = Phone[873555333]
8 sẽ trả về
class Phone:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
pn = Phone[873555333]
9, mặc dù chúng tôi đang so sánh số điện thoại với số tài khoản ngân hàng.Đó là thực tế tốt để kiểm tra lớp đối tượng được truyền đến phương pháp
class BankAccount:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
acct = BankAccount[873555333]
0 để đảm bảo so sánh có ý nghĩa. Xem bây giờ hướng dẫn này có một khóa học video liên quan được tạo bởi nhóm Python thực sự. Xem nó cùng với hướng dẫn bằng văn bản để làm sâu sắc thêm sự hiểu biết của bạn: So sánh các đối tượng Python đúng cách: "là" vs "==" This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Comparing Python Objects the Right Way: "is" vs "=="
Không phải VS! = Python?
Toán tử
class Phone:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
pn = Phone[873555333]
1 so sánh giá trị hoặc bình đẳng của hai đối tượng, trong khi toán tử Python
class BankAccount:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
acct = BankAccount[873555333]
1 kiểm tra xem hai biến có hướng đến cùng một đối tượng trong bộ nhớ hay không. Trong phần lớn các trường hợp, điều này có nghĩa là bạn nên sử dụng các toán tử bình đẳng
class Phone:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
pn = Phone[873555333]
1 và
class BankAccount:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
acct = BankAccount[873555333]
9, ngoại trừ khi bạn so sánh với >>> help[id]
Help on built-in function id in module builtins:
id[obj, /]
Return the identity of an object.
This is guaranteed to be unique among simultaneously existing objects.
[CPython uses the object's memory address.]
>>> id[id]
2570892442576
0.equality of two objects, whereas the Python
class BankAccount:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
acct = BankAccount[873555333]
1 operator checks whether two variables point to the same object in memory. In the vast majority of cases, this means you should use the equality operators
class Phone:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
pn = Phone[873555333]
1 and
class BankAccount:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
acct = BankAccount[873555333]
9, except when you’re comparing to >>> help[id]
Help on built-in function id in module builtins:
id[obj, /]
Return the identity of an object.
This is guaranteed to be unique among simultaneously existing objects.
[CPython uses the object's memory address.]
>>> id[id]
2570892442576
0. Trong hướng dẫn này, bạn sẽ học:
- Sự khác biệt là gì giữa bình đẳng đối tượng và bản sắcobject equality and identity
- Khi nào nên sử dụng các toán tử bình đẳng và nhận dạng để so sánh các đối tượngcompare objects
- Những người điều khiển python này làm gì dưới mui xePython operators do under the hood
- Tại sao sử dụng
1 vàclass BankAccount: def __init__[self, number]: self.number = number def __eq__[self, other]: return self.number == \ other.number acct = BankAccount[873555333]
2 để so sánh các giá trị dẫn đến hành vi bất ngờunexpected behavior>>> help[id] Help on built-in function id in module builtins: id[obj, /] Return the identity of an object. This is guaranteed to be unique among simultaneously existing objects. [CPython uses the object's memory address.] >>> id[id] 2570892442576
- Cách viết phương thức lớp tùy chỉnh
0 để xác định hành vi của người vận hành bình đẳngcustomclass BankAccount: def __init__[self, number]: self.number = number def __eq__[self, other]: return self.number == \ other.number acct = BankAccount[873555333]
0 class method to define equality operator behaviorclass BankAccount: def __init__[self, number]: self.number = number def __eq__[self, other]: return self.number == \ other.number acct = BankAccount[873555333]
So sánh danh tính với Python IS và không phải là người vận hành
Các toán tử Python
class BankAccount:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
acct = BankAccount[873555333]
1 và >>> help[id]
Help on built-in function id in module builtins:
id[obj, /]
Return the identity of an object.
This is guaranteed to be unique among simultaneously existing objects.
[CPython uses the object's memory address.]
>>> id[id]
2570892442576
2 so sánh danh tính của hai đối tượng. Trong Cpython, đây là địa chỉ bộ nhớ của họ. Tất cả mọi thứ trong Python là một đối tượng và mỗi đối tượng được lưu trữ tại một vị trí bộ nhớ cụ thể. Các toán tử Python
class BankAccount:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
acct = BankAccount[873555333]
1 và >>> help[id]
Help on built-in function id in module builtins:
id[obj, /]
Return the identity of an object.
This is guaranteed to be unique among simultaneously existing objects.
[CPython uses the object's memory address.]
>>> id[id]
2570892442576
2 kiểm tra xem hai biến có đề cập đến cùng một đối tượng trong bộ nhớ hay không.identity of two objects. In
CPython, this is their memory address. Everything in Python is an object, and each object is stored at a specific memory location. The Python
class BankAccount:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
acct = BankAccount[873555333]
1 and >>> help[id]
Help on built-in function id in module builtins:
id[obj, /]
Return the identity of an object.
This is guaranteed to be unique among simultaneously existing objects.
[CPython uses the object's memory address.]
>>> id[id]
2570892442576
2 operators check whether two variables refer to the same object in
memory. Bạn có thể sử dụng
>>> help[id]
Help on built-in function id in module builtins:
id[obj, /]
Return the identity of an object.
This is guaranteed to be unique among simultaneously existing objects.
[CPython uses the object's memory address.]
>>> id[id]
2570892442576
8 để kiểm tra danh tính của một đối tượng:>>>
>>> help[id]
Help on built-in function id in module builtins:
id[obj, /]
Return the identity of an object.
This is guaranteed to be unique among simultaneously existing objects.
[CPython uses the object's memory address.]
>>> id[id]
2570892442576
Dòng cuối cùng hiển thị địa chỉ bộ nhớ nơi chính chức năng tích hợp
>>> help[id]
Help on built-in function id in module builtins:
id[obj, /]
Return the identity of an object.
This is guaranteed to be unique among simultaneously existing objects.
[CPython uses the object's memory address.]
>>> id[id]
2570892442576
9 được lưu trữ.Có một số trường hợp phổ biến trong đó các đối tượng có cùng giá trị sẽ có cùng một ID theo mặc định. Ví dụ, các số -5 đến 256 được thực tập trong CPython. Mỗi số được lưu trữ tại một vị trí số ít và cố định trong bộ nhớ, lưu bộ nhớ cho các số nguyên thường được sử dụng.interned in CPython. Each number is stored at a singular and fixed place in memory, which saves memory for commonly-used integers.
Bạn có thể sử dụng
>>> from sys import intern
>>> a = 'hello world'
>>> b = 'hello world'
>>> a is b
False
>>> id[a]
1603648396784
>>> id[b]
1603648426160
>>> a = intern[a]
>>> b = intern[b]
>>> a is b
True
>>> id[a]
1603648396784
>>> id[b]
1603648396784
0 cho chuỗi thực tập để thực hiện. Chức năng này cho phép bạn so sánh các địa chỉ bộ nhớ của chúng thay vì so sánh các đặc điểm của các chuỗi:>>>
>>> from sys import intern
>>> a = 'hello world'
>>> b = 'hello world'
>>> a is b
False
>>> id[a]
1603648396784
>>> id[b]
1603648426160
>>> a = intern[a]
>>> b = intern[b]
>>> a is b
True
>>> id[a]
1603648396784
>>> id[b]
1603648396784
Dòng cuối cùng hiển thị địa chỉ bộ nhớ nơi chính chức năng tích hợp
>>> help[id]
Help on built-in function id in module builtins:
id[obj, /]
Return the identity of an object.
This is guaranteed to be unique among simultaneously existing objects.
[CPython uses the object's memory address.]
>>> id[id]
2570892442576
9 được lưu trữ.Có một số trường hợp phổ biến trong đó các đối tượng có cùng giá trị sẽ có cùng một ID theo mặc định. Ví dụ, các số -5 đến 256 được thực tập trong CPython. Mỗi số được lưu trữ tại một vị trí số ít và cố định trong bộ nhớ, lưu bộ nhớ cho các số nguyên thường được sử dụng.This means you should not use the Python
1 operator to compare values.
class BankAccount:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
acct = BankAccount[873555333]
Bạn có thể sử dụng >>> from sys import intern
>>> a = 'hello world'
>>> b = 'hello world'
>>> a is b
False
>>> id[a]
1603648396784
>>> id[b]
1603648426160
>>> a = intern[a]
>>> b = intern[b]
>>> a is b
True
>>> id[a]
1603648396784
>>> id[b]
1603648396784
0 cho chuỗi thực tập để thực hiện. Chức năng này cho phép bạn so sánh các địa chỉ bộ nhớ của chúng thay vì so sánh các đặc điểm của các chuỗi:
>>> from sys import intern
>>> a = 'hello world'
>>> b = 'hello world'
>>> a is b
False
>>> id[a]
1603648396784
>>> id[b]
1603648426160
>>> a = intern[a]
>>> b = intern[b]
>>> a is b
True
>>> id[a]
1603648396784
>>> id[b]
1603648396784
Các biến
>>> from sys import intern
>>> a = 'hello world'
>>> b = 'hello world'
>>> a is b
False
>>> id[a]
1603648396784
>>> id[b]
1603648426160
>>> a = intern[a]
>>> b = intern[b]
>>> a is b
True
>>> id[a]
1603648396784
>>> id[b]
1603648396784
1 và >>> from sys import intern
>>> a = 'hello world'
>>> b = 'hello world'
>>> a is b
False
>>> id[a]
1603648396784
>>> id[b]
1603648426160
>>> a = intern[a]
>>> b = intern[b]
>>> a is b
True
>>> id[a]
1603648396784
>>> id[b]
1603648396784
2 ban đầu chỉ ra hai đối tượng khác nhau trong bộ nhớ, như được hiển thị bởi ID khác nhau của chúng. Khi bạn thực tập chúng, bạn đảm bảo rằng >>> from sys import intern
>>> a = 'hello world'
>>> b = 'hello world'
>>> a is b
False
>>> id[a]
1603648396784
>>> id[b]
1603648426160
>>> a = intern[a]
>>> b = intern[b]
>>> a is b
True
>>> id[a]
1603648396784
>>> id[b]
1603648396784
1 và >>> from sys import intern
>>> a = 'hello world'
>>> b = 'hello world'
>>> a is b
False
>>> id[a]
1603648396784
>>> id[b]
1603648426160
>>> a = intern[a]
>>> b = intern[b]
>>> a is b
True
>>> id[a]
1603648396784
>>> id[b]
1603648396784
2 trỏ đến cùng một đối tượng trong bộ nhớ. Bất kỳ chuỗi mới nào có giá trị >>> from sys import intern
>>> a = 'hello world'
>>> b = 'hello world'
>>> a is b
False
>>> id[a]
1603648396784
>>> id[b]
1603648426160
>>> a = intern[a]
>>> b = intern[b]
>>> a is b
True
>>> id[a]
1603648396784
>>> id[b]
1603648396784
5 bây giờ sẽ được tạo tại một vị trí bộ nhớ mới, nhưng khi bạn thực tập chuỗi mới này, bạn đảm bảo rằng nó chỉ vào cùng một địa chỉ bộ nhớ với >>> from sys import intern
>>> a = 'hello world'
>>> b = 'hello world'
>>> a is b
False
>>> id[a]
1603648396784
>>> id[b]
1603648426160
>>> a = intern[a]
>>> b = intern[b]
>>> a is b
True
>>> id[a]
1603648396784
>>> id[b]
1603648396784
5 đầu tiên mà bạn đã thực tập.>>>
>>> a = 256
>>> b = 256
>>> a is b
True
>>> id[a]
1638894624
>>> id[b]
1638894624
>>> a = 257
>>> b = 257
>>> a is b
False
>>> id[a]
2570926051952
>>> id[b]
2570926051984
Dòng cuối cùng hiển thị địa chỉ bộ nhớ nơi chính chức năng tích hợp
>>> help[id]
Help on built-in function id in module builtins:
id[obj, /]
Return the identity of an object.
This is guaranteed to be unique among simultaneously existing objects.
[CPython uses the object's memory address.]
>>> id[id]
2570892442576
9 được lưu trữ.common integers [ranging from -5 to 256], they’re stored at separate memory addresses.Có một số trường hợp phổ biến trong đó các đối tượng có cùng giá trị sẽ có cùng một ID theo mặc định. Ví dụ, các số -5 đến 256 được thực tập trong CPython. Mỗi số được lưu trữ tại một vị trí số ít và cố định trong bộ nhớ, lưu bộ nhớ cho các số nguyên thường được sử dụng.
Bạn có thể sử dụng
>>> from sys import intern
>>> a = 'hello world'
>>> b = 'hello world'
>>> a is b
False
>>> id[a]
1603648396784
>>> id[b]
1603648426160
>>> a = intern[a]
>>> b = intern[b]
>>> a is b
True
>>> id[a]
1603648396784
>>> id[b]
1603648396784
0 cho chuỗi thực tập để thực hiện. Chức năng này cho phép bạn so sánh các địa chỉ bộ nhớ của chúng thay vì so sánh các đặc điểm của các chuỗi:>>>
>>> a = [1, 2, 3]
>>> b = a
>>> a
[1, 2, 3]
>>> b
[1, 2, 3]
>>> a.append[4]
>>> a
[1, 2, 3, 4]
>>> b
[1, 2, 3, 4]
>>> id[a]
2570926056520
>>> id[b]
2570926056520
Dòng cuối cùng hiển thị địa chỉ bộ nhớ nơi chính chức năng tích hợp
>>> help[id]
Help on built-in function id in module builtins:
id[obj, /]
Return the identity of an object.
This is guaranteed to be unique among simultaneously existing objects.
[CPython uses the object's memory address.]
>>> id[id]
2570892442576
9 được lưu trữ.Có một số trường hợp phổ biến trong đó các đối tượng có cùng giá trị sẽ có cùng một ID theo mặc định. Ví dụ, các số -5 đến 256 được thực tập trong CPython. Mỗi số được lưu trữ tại một vị trí số ít và cố định trong bộ nhớ, lưu bộ nhớ cho các số nguyên thường được sử dụng.
>>>
>>> a = [1, 2, 3]
>>> b = [1, 2, 3]
>>> a is b
False
>>> id[a]
2356388925576
>>> id[b]
2356388952648
Dòng cuối cùng hiển thị địa chỉ bộ nhớ nơi chính chức năng tích hợp
>>> help[id]
Help on built-in function id in module builtins:
id[obj, /]
Return the identity of an object.
This is guaranteed to be unique among simultaneously existing objects.
[CPython uses the object's memory address.]
>>> id[id]
2570892442576
9 được lưu trữ.Có một số trường hợp phổ biến trong đó các đối tượng có cùng giá trị sẽ có cùng một ID theo mặc định. Ví dụ, các số -5 đến 256 được thực tập trong CPython. Mỗi số được lưu trữ tại một vị trí số ít và cố định trong bộ nhớ, lưu bộ nhớ cho các số nguyên thường được sử dụng.
Bạn có thể sử dụng
>>> from sys import intern
>>> a = 'hello world'
>>> b = 'hello world'
>>> a is b
False
>>> id[a]
1603648396784
>>> id[b]
1603648426160
>>> a = intern[a]
>>> b = intern[b]
>>> a is b
True
>>> id[a]
1603648396784
>>> id[b]
1603648396784
0 cho chuỗi thực tập để thực hiện. Chức năng này cho phép bạn so sánh các địa chỉ bộ nhớ của chúng thay vì so sánh các đặc điểm của các chuỗi:same value are often stored at separate memory addresses. Use the equality operators
class Phone:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
pn = Phone[873555333]
1 and
class BankAccount:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
acct = BankAccount[873555333]
9 if you want to check whether or not two objects have the same value, regardless of where they’re stored in memory. In the vast majority of cases, this is
what you want to do.Các biến >>> from sys import intern
>>> a = 'hello world'
>>> b = 'hello world'
>>> a is b
False
>>> id[a]
1603648396784
>>> id[b]
1603648426160
>>> a = intern[a]
>>> b = intern[b]
>>> a is b
True
>>> id[a]
1603648396784
>>> id[b]
1603648396784
1 và >>> from sys import intern
>>> a = 'hello world'
>>> b = 'hello world'
>>> a is b
False
>>> id[a]
1603648396784
>>> id[b]
1603648426160
>>> a = intern[a]
>>> b = intern[b]
>>> a is b
True
>>> id[a]
1603648396784
>>> id[b]
1603648396784
2 ban đầu chỉ ra hai đối tượng khác nhau trong bộ nhớ, như được hiển thị bởi ID khác nhau của chúng. Khi bạn thực tập chúng, bạn đảm bảo rằng >>> from sys import intern
>>> a = 'hello world'
>>> b = 'hello world'
>>> a is b
False
>>> id[a]
1603648396784
>>> id[b]
1603648426160
>>> a = intern[a]
>>> b = intern[b]
>>> a is b
True
>>> id[a]
1603648396784
>>> id[b]
1603648396784
1 và >>> from sys import intern
>>> a = 'hello world'
>>> b = 'hello world'
>>> a is b
False
>>> id[a]
1603648396784
>>> id[b]
1603648426160
>>> a = intern[a]
>>> b = intern[b]
>>> a is b
True
>>> id[a]
1603648396784
>>> id[b]
1603648396784
2 trỏ đến cùng một đối tượng trong bộ nhớ. Bất kỳ chuỗi mới nào có giá trị >>> from sys import intern
>>> a = 'hello world'
>>> b = 'hello world'
>>> a is b
False
>>> id[a]
1603648396784
>>> id[b]
1603648426160
>>> a = intern[a]
>>> b = intern[b]
>>> a is b
True
>>> id[a]
1603648396784
>>> id[b]
1603648396784
5 bây giờ sẽ được tạo tại một vị trí bộ nhớ mới, nhưng khi bạn thực tập chuỗi mới này, bạn đảm bảo rằng nó chỉ vào cùng một địa chỉ bộ nhớ với >>> from sys import intern
>>> a = 'hello world'
>>> b = 'hello world'
>>> a is b
False
>>> id[a]
1603648396784
>>> id[b]
1603648426160
>>> a = intern[a]
>>> b = intern[b]
>>> a is b
True
>>> id[a]
1603648396784
>>> id[b]
1603648396784
5 đầu tiên mà bạn đã thực tập.
>>> from sys import intern
>>> a = 'hello world'
>>> b = 'hello world'
>>> a is b
False
>>> id[a]
1603648396784
>>> id[b]
1603648426160
>>> a = intern[a]
>>> b = intern[b]
>>> a is b
True
>>> id[a]
1603648396784
>>> id[b]
1603648396784
>>> from sys import intern
>>> a = 'hello world'
>>> b = 'hello world'
>>> a is b
False
>>> id[a]
1603648396784
>>> id[b]
1603648426160
>>> a = intern[a]
>>> b = intern[b]
>>> a is b
True
>>> id[a]
1603648396784
>>> id[b]
1603648396784
>>> from sys import intern
>>> a = 'hello world'
>>> b = 'hello world'
>>> a is b
False
>>> id[a]
1603648396784
>>> id[b]
1603648426160
>>> a = intern[a]
>>> b = intern[b]
>>> a is b
True
>>> id[a]
1603648396784
>>> id[b]
1603648396784
>>> from sys import intern
>>> a = 'hello world'
>>> b = 'hello world'
>>> a is b
False
>>> id[a]
1603648396784
>>> id[b]
1603648426160
>>> a = intern[a]
>>> b = intern[b]
>>> a is b
True
>>> id[a]
1603648396784
>>> id[b]
1603648396784
>>> from sys import intern
>>> a = 'hello world'
>>> b = 'hello world'
>>> a is b
False
>>> id[a]
1603648396784
>>> id[b]
1603648426160
>>> a = intern[a]
>>> b = intern[b]
>>> a is b
True
>>> id[a]
1603648396784
>>> id[b]
1603648396784
>>> from sys import intern
>>> a = 'hello world'
>>> b = 'hello world'
>>> a is b
False
>>> id[a]
1603648396784
>>> id[b]
1603648426160
>>> a = intern[a]
>>> b = intern[b]
>>> a is b
True
>>> id[a]
1603648396784
>>> id[b]
1603648396784
Các đối tượng khác được thực tập theo mặc định là
>>> help[id]
Help on built-in function id in module builtins:
id[obj, /]
Return the identity of an object.
This is guaranteed to be unique among simultaneously existing objects.
[CPython uses the object's memory address.]
>>> id[id]
2570892442576
0,
class Phone:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
pn = Phone[873555333]
9, >>> from sys import intern
>>> a = 'hello world'
>>> b = 'hello world'
>>> a is b
False
>>> id[a]
1603648396784
>>> id[b]
1603648426160
>>> a = intern[a]
>>> b = intern[b]
>>> a is b
True
>>> id[a]
1603648396784
>>> id[b]
1603648396784
9 và các chuỗi đơn giản. Hãy nhớ rằng hầu hết thời gian, các đối tượng khác nhau có cùng giá trị sẽ được lưu trữ tại các địa chỉ bộ nhớ riêng biệt. Điều này có nghĩa là bạn không nên sử dụng toán tử Python
class BankAccount:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
acct = BankAccount[873555333]
1 để so sánh các giá trị.>>>
>>> a = [1, 2, 3]
>>> b = a.copy[]
>>> a
[1, 2, 3]
>>> b
[1, 2, 3]
>>> a == b
True
>>> a is b
False
>>> id[a]
2570926058312
>>> id[b]
2570926057736
Dòng cuối cùng hiển thị địa chỉ bộ nhớ nơi chính chức năng tích hợp
>>> help[id]
Help on built-in function id in module builtins:
id[obj, /]
Return the identity of an object.
This is guaranteed to be unique among simultaneously existing objects.
[CPython uses the object's memory address.]
>>> id[id]
2570892442576
9 được lưu trữ.Cách so sánh bằng cách bình đẳng hoạt động
Sự kỳ diệu của toán tử bình đẳng
class Phone:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
pn = Phone[873555333]
1 xảy ra trong phương pháp lớp
class BankAccount:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
acct = BankAccount[873555333]
0 của đối tượng ở bên trái của dấu hiệu
class Phone:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
pn = Phone[873555333]
1.Đây là một phương pháp lớp ma thuật mà Lát gọi là bất cứ khi nào một thể hiện của lớp này được so sánh với một đối tượng khác. Nếu phương thức này không được thực hiện, thì
class Phone:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
pn = Phone[873555333]
1 sẽ so sánh các địa chỉ bộ nhớ của hai đối tượng theo mặc định.Là một bài tập, hãy tạo một lớp
>>> a = [1, 2, 3]
>>> b = [1, 2, 3]
>>> a is b
False
>>> id[a]
2356388925576
>>> id[b]
2356388952648
5 kế thừa từ >>> a = [1, 2, 3]
>>> b = [1, 2, 3]
>>> a is b
False
>>> id[a]
2356388925576
>>> id[b]
2356388952648
6 và triển khai
class BankAccount:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
acct = BankAccount[873555333]
0 để so sánh xem độ dài của chuỗi này có giống với độ dài của đối tượng khác hay không:class SillyString[str]:
# This method gets called when using == on the object
def __eq__[self, other]:
print[f'comparing {self} to {other}']
# Return True if self and other have the same length
return len[self] == len[other]
Bây giờ, một sillystring
>>> from sys import intern
>>> a = 'hello world'
>>> b = 'hello world'
>>> a is b
False
>>> id[a]
1603648396784
>>> id[b]
1603648426160
>>> a = intern[a]
>>> b = intern[b]
>>> a is b
True
>>> id[a]
1603648396784
>>> id[b]
1603648396784
5 phải bằng với chuỗi >>> a = [1, 2, 3]
>>> b = [1, 2, 3]
>>> a is b
False
>>> id[a]
2356388925576
>>> id[b]
2356388952648
9 và thậm chí với bất kỳ đối tượng nào khác có cùng độ dài:
>>>
class Phone:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
pn = Phone[873555333]
0Tất nhiên, đây là hành vi ngớ ngẩn đối với một đối tượng hoạt động như một chuỗi, nhưng nó minh họa những gì xảy ra khi bạn so sánh hai đối tượng sử dụng
class Phone:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
pn = Phone[873555333]
1. Toán tử
class BankAccount:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
acct = BankAccount[873555333]
9 đưa ra phản hồi nghịch đảo của điều này trừ khi phương pháp lớp >>> a = [1, 2, 3]
>>> b = a.copy[]
>>> a
[1, 2, 3]
>>> b
[1, 2, 3]
>>> a == b
True
>>> a is b
False
>>> id[a]
2570926058312
>>> id[b]
2570926057736
2 cụ thể được thực hiện.Ví dụ trên cũng cho bạn thấy rõ lý do tại sao việc sử dụng toán tử Python
class BankAccount:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
acct = BankAccount[873555333]
1 là tốt để so sánh với >>> help[id]
Help on built-in function id in module builtins:
id[obj, /]
Return the identity of an object.
This is guaranteed to be unique among simultaneously existing objects.
[CPython uses the object's memory address.]
>>> id[id]
2570892442576
0, thay vì toán tử
class Phone:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
pn = Phone[873555333]
1. Nó không chỉ nhanh hơn vì nó so sánh các địa chỉ bộ nhớ, mà còn an toàn hơn vì nó không phụ thuộc vào logic của bất kỳ phương pháp lớp
class BankAccount:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
acct = BankAccount[873555333]
0 nào.So sánh các toán tử so sánh Python
Theo nguyên tắc thông thường, bạn nên luôn luôn sử dụng các toán tử bình đẳng
class Phone:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
pn = Phone[873555333]
1 và
class BankAccount:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
acct = BankAccount[873555333]
9, ngoại trừ khi bạn so sánh với >>> help[id]
Help on built-in function id in module builtins:
id[obj, /]
Return the identity of an object.
This is guaranteed to be unique among simultaneously existing objects.
[CPython uses the object's memory address.]
>>> id[id]
2570892442576
0:Sử dụng các toán tử Python
1 vàclass Phone: def __init__[self, number]: self.number = number def __eq__[self, other]: return self.number == \ other.number pn = Phone[873555333]
9 để so sánh bình đẳng đối tượng. Ở đây, bạn thường so sánh giá trị của hai đối tượng. Đây là những gì bạn cần nếu bạn muốn so sánh việc hai đối tượng có cùng nội dung hay không, và bạn không quan tâm đến nơi họ lưu trữ trong bộ nhớ.. Here, you’re generally comparing the value of two objects. This is what you need if you want to compare whether or not two objects have the same contents, and you don’t care about where they’re stored in memory.class BankAccount: def __init__[self, number]: self.number = number def __eq__[self, other]: return self.number == \ other.number acct = BankAccount[873555333]
Sử dụng các toán tử Python
1 vàclass BankAccount: def __init__[self, number]: self.number = number def __eq__[self, other]: return self.number == \ other.number acct = BankAccount[873555333]
2 khi bạn muốn so sánh nhận dạng đối tượng. Ở đây, bạn có thể so sánh việc hai biến có chỉ vào cùng một đối tượng trong bộ nhớ hay không. Trường hợp sử dụng chính cho các nhà khai thác này là khi bạn so sánh với>>> help[id] Help on built-in function id in module builtins: id[obj, /] Return the identity of an object. This is guaranteed to be unique among simultaneously existing objects. [CPython uses the object's memory address.] >>> id[id] 2570892442576
0. Nó nhanh hơn và an toàn hơn để so sánh với>>> help[id] Help on built-in function id in module builtins: id[obj, /] Return the identity of an object. This is guaranteed to be unique among simultaneously existing objects. [CPython uses the object's memory address.] >>> id[id] 2570892442576
0 theo địa chỉ bộ nhớ so với sử dụng các phương thức lớp.. Here, you’re comparing whether or not two variables point to the same object in memory. The main use case for these operators is when you’re comparing to>>> help[id] Help on built-in function id in module builtins: id[obj, /] Return the identity of an object. This is guaranteed to be unique among simultaneously existing objects. [CPython uses the object's memory address.] >>> id[id] 2570892442576
0. It’s faster and safer to compare to>>> help[id] Help on built-in function id in module builtins: id[obj, /] Return the identity of an object. This is guaranteed to be unique among simultaneously existing objects. [CPython uses the object's memory address.] >>> id[id] 2570892442576
0 by memory address than it is by using class methods.>>> help[id] Help on built-in function id in module builtins: id[obj, /] Return the identity of an object. This is guaranteed to be unique among simultaneously existing objects. [CPython uses the object's memory address.] >>> id[id] 2570892442576
Các biến có cùng giá trị thường được lưu trữ tại các địa chỉ bộ nhớ riêng biệt. Điều này có nghĩa là bạn nên sử dụng
class Phone:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
pn = Phone[873555333]
1 và
class BankAccount:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
acct = BankAccount[873555333]
9 để so sánh các giá trị của chúng và chỉ sử dụng các toán tử Python
class BankAccount:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
acct = BankAccount[873555333]
1 và >>> help[id]
Help on built-in function id in module builtins:
id[obj, /]
Return the identity of an object.
This is guaranteed to be unique among simultaneously existing objects.
[CPython uses the object's memory address.]
>>> id[id]
2570892442576
2 khi bạn muốn kiểm tra xem hai biến chỉ vào cùng một địa chỉ bộ nhớ.Sự kết luận
Trong hướng dẫn này, bạn đã học được rằng
class Phone:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
pn = Phone[873555333]
1 và
class BankAccount:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
acct = BankAccount[873555333]
9 so sánh giá trị của hai đối tượng, trong khi các toán tử Python
class BankAccount:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
acct = BankAccount[873555333]
1 và >>> help[id]
Help on built-in function id in module builtins:
id[obj, /]
Return the identity of an object.
This is guaranteed to be unique among simultaneously existing objects.
[CPython uses the object's memory address.]
>>> id[id]
2570892442576
2 so sánh liệu hai biến có đề cập đến cùng một đối tượng trong bộ nhớ hay không. Nếu bạn ghi nhớ sự khác biệt này, thì bạn sẽ có thể ngăn chặn hành vi bất ngờ trong mã của mình.compare the value of two objects, whereas the Python
class BankAccount:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
acct = BankAccount[873555333]
1 and >>> help[id]
Help on built-in function id in module builtins:
id[obj, /]
Return the identity of an object.
This is guaranteed to be unique among simultaneously existing objects.
[CPython uses the object's memory address.]
>>> id[id]
2570892442576
2 operators compare whether two variables
refer to the same object in memory. If you keep this distinction in mind, then you should be able to prevent unexpected behavior in your code.Nếu bạn muốn đọc thêm về thế giới tuyệt vời của việc thực tập đối tượng và nhà điều hành Python
class BankAccount:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
acct = BankAccount[873555333]
1, thì hãy xem lý do tại sao bạn gần như không bao giờ nên sử dụng trên mạng trong Python. Bạn cũng có thể xem cách bạn có thể sử dụng >>> from sys import intern
>>> a = 'hello world'
>>> b = 'hello world'
>>> a is b
False
>>> id[a]
1603648396784
>>> id[b]
1603648426160
>>> a = intern[a]
>>> b = intern[b]
>>> a is b
True
>>> id[a]
1603648396784
>>> id[b]
1603648396784
0 để tối ưu hóa thời gian sử dụng bộ nhớ và thời gian so sánh cho các chuỗi, mặc dù rất có thể Python đã tự động xử lý việc này cho bạn hậu trường.object interning and the Python
class BankAccount:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
acct = BankAccount[873555333]
1 operator, then check out Why you should almost never use “is” in Python. You could also have a look at how you can
use >>> from sys import intern
>>> a = 'hello world'
>>> b = 'hello world'
>>> a is b
False
>>> id[a]
1603648396784
>>> id[b]
1603648426160
>>> a = intern[a]
>>> b = intern[b]
>>> a is b
True
>>> id[a]
1603648396784
>>> id[b]
1603648396784
0 to optimize memory usage and comparison times for strings, although the chances are that Python already automatically handles this for you behind-the-scenes.Bây giờ, bạn đã học được những gì các nhà khai thác bình đẳng và nhận dạng làm dưới mui xe, bạn có thể thử viết các phương thức lớp
class BankAccount:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
acct = BankAccount[873555333]
0 của riêng mình, xác định cách so sánh của lớp này khi sử dụng toán tử
class Phone:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
pn = Phone[873555333]
1. Đi và áp dụng kiến thức mới của bạn về các nhà khai thác so sánh Python này!equality and identity operators do under the hood, you can try writing your own
class BankAccount:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
acct = BankAccount[873555333]
0 class methods, which define how instances of this class are compared when using the
class Phone:
def __init__[self, number]:
self.number = number
def __eq__[self, other]:
return self.number == \
other.number
pn = Phone[873555333]
1 operator. Go and apply your newfound knowledge of these Python comparison operators!
Xem bây giờ hướng dẫn này có một khóa học video liên quan được tạo bởi nhóm Python thực sự. Xem nó cùng với hướng dẫn bằng văn bản để làm sâu sắc thêm sự hiểu biết của bạn: So sánh các đối tượng Python đúng cách: "là" vs "==" This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Comparing Python Objects the Right Way: "is" vs "=="