Operator overloading: การเปรียบเทียบออบเจกต์

Python เบื้องต้นสำหรับการเขียนโปรแกรมเชิงวัตถุ

George Boorman

Curriculum Manager, DataCamp

ความเท่ากันของออบเจกต์

class Customer:
    def __init__(self, name, balance):
        self.name, self.balance = name, balance  

customer1 = Customer("Maryam Azar", 3000)
customer2 = Customer("Maryam Azar", 3000)

# Check for equality customer1 == customer2
False
Python เบื้องต้นสำหรับการเขียนโปรแกรมเชิงวัตถุ

ความเท่ากันของออบเจกต์

class Customer:
    def __init__(self, name, balance, acc_id):
        self.name, self.balance = name, balance
        self.acc_id = acc_id    

customer1 = Customer("Maryam Azar", 3000, 123)
customer2 = Customer("Maryam Azar", 3000, 123)

customer1 == customer2
False
Python เบื้องต้นสำหรับการเขียนโปรแกรมเชิงวัตถุ

ตัวแปรคือ reference

customer_one = Customer("Maryam Azar", 3000, 123)
customer_two = Customer("Maryam Azar", 3000, 123)

print(customer_one)
<__main__.Customer at 0x1f8598e2e48>
print(customer_two)
<__main__.Customer at 0x1f8598e2240>
  • ผลลัพธ์ของ print() อ้างอิงถึงตำแหน่งหน่วยความจำที่ตัวแปรชี้ไป
  • == เปรียบเทียบ reference ไม่ใช่ข้อมูล
Python เบื้องต้นสำหรับการเขียนโปรแกรมเชิงวัตถุ

การเปรียบเทียบแบบกำหนดเอง

# Two different lists containing the same data
list_one = [1,2,3]
list_two = [1,2,3]

list_one == list_two
True
Python เบื้องต้นสำหรับการเขียนโปรแกรมเชิงวัตถุ

เมธอด __eq__()

  • __eq__() ถูกเรียกเมื่อเปรียบเทียบออบเจกต์ 2 ตัวในคลาสด้วย ==
  • รับอาร์กิวเมนต์ 2 ตัว คือ self และ other ซึ่งเป็นออบเจกต์ที่จะเปรียบเทียบ
  • คืนค่าเป็น Boolean
Python เบื้องต้นสำหรับการเขียนโปรแกรมเชิงวัตถุ

เมธอด __eq__()

class Customer:
    def __init__(self, acc_id, name):
        self.acc_id, self.name = acc_id, name

# Will be called when == is used def __eq__(self, other):
# Printout print("__eq__() is called") # Returns True if all attributes match return (self.acc_id == other.acc_id) and (self.name == other.name)
Python เบื้องต้นสำหรับการเขียนโปรแกรมเชิงวัตถุ

การเปรียบเทียบออบเจกต์

# Two equal objects
customer1 = Customer(123, "Maryam Azar")
customer2 = Customer(123, "Maryam Azar")

customer1 == customer2
__eq__() is called
True
# Two unequal objects - different ids
customer1 = Customer(123, "Maryam Azar")
customer2 = Customer(456, "Maryam Azar")

customer1 == customer2
__eq__() is called
False
Python เบื้องต้นสำหรับการเขียนโปรแกรมเชิงวัตถุ

การตรวจสอบประเภท

  • ถ้าออบเจกต์จากคลาสต่างกันมี attribute และค่าเหมือนกันจะเกิดอะไรขึ้น?
    • Python จะประเมินว่าเท่ากัน
class Customer:
    def __init__(self, acc_id, name):
        self.acc_id, self.name = idacc_id name

    def __eq__(self, other):
        # Returns True if the objects have the same attributes
        # and are of the same type
        return (self.acc_id == other.acc_id) and (self.name == other.name)\
            and (type(self) == type(other))
Python เบื้องต้นสำหรับการเขียนโปรแกรมเชิงวัตถุ

ตัวดำเนินการเปรียบเทียบอื่นๆ

Operator Method
== __eq__()
!= __ne__()
>= __ge__()
<= __le__()
> __gt__()
< __lt__()
  • ปรับแต่งได้โดยกำหนดเมธอดภายในคลาส
Python เบื้องต้นสำหรับการเขียนโปรแกรมเชิงวัตถุ

มาฝึกกันเถอะ!

Python เบื้องต้นสำหรับการเขียนโปรแกรมเชิงวัตถุ

Preparing Video For Download...