การเปรียบเทียบการสืบทอดและการแสดงผลแบบสตริง

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

George Boorman

Curriculum Manager, DataCamp

การเปรียบเทียบออบเจกต์จากคลาสต่างกัน

class Customer:
    def __init__(self, acc_id, name):
        self.acc_id, self.name = acc_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 เบื้องต้นสำหรับการเขียนโปรแกรมเชิงวัตถุ

การเปรียบเทียบออบเจกต์ที่มีการสืบทอด

 

 

 

                               

    จะเกิดอะไรขึ้นถ้าออบเจกต์หนึ่ง สืบทอด มาจากคลาสของอีกออบเจกต์?

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

บัญชีธนาคารและบัญชีออมทรัพย์

SavingsAccount สืบทอดมาจาก BankAccount

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

`__eq__` ในคลาสแม่/คลาสลูก

class BankAccount:
    def __init__(self, number, balance=0):
        self.balance = balance
        self.number = number

    def withdraw(self, amount):
        self.balance -= amount 

    # Define __eq__ that returns True 
    # if the number attributes are equal 
    def __eq__(self, other):
        print("BankAccount __eq__() called")
        return self.number == other.number 
class SavingsAccount(BankAccount):
    def __init__(self, number, balance, interest_rate):
        BankAccount.__init__(self, number,
                             balance)
          self.interest_rate = interest_rate

    # Define __eq__ that returns True 
    # if the number attributes are equal 
    def __eq__(self, other):
        print("SavingsAccount __eq__() called")
        return self.number == other.number 
Python เบื้องต้นสำหรับการเขียนโปรแกรมเชิงวัตถุ

การเปรียบเทียบออบเจกต์คลาสแม่และคลาสลูก

ba = BankAccount(123, 10000)
sa = SavingsAccount(456, 2000, 0.05)
# Compare the two objects
ba == sa
SavingsAccount __eq__() called
False
sa == ba
SavingsAccount __eq__() called
False
Python เบื้องต้นสำหรับการเขียนโปรแกรมเชิงวัตถุ

การพิมพ์ออบเจกต์

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

cust = Customer("Maryam Azar", 3000)
print(cust)
<__main__.Customer at 0x1f8598e2240>




a_list = [1,2,3]
print(a_list)
[1, 2, 3]
Python เบื้องต้นสำหรับการเขียนโปรแกรมเชิงวัตถุ

__str__()

  • print(obj), str(obj)
print(np.array([1,2,3]))
[1 2 3]
str(np.array([1,2,3]))
'[1 2 3]'
  • ไม่เป็นทางการ สำหรับผู้ใช้งานทั่วไป
  • การแสดงผลแบบ string

__repr__()

  • repr(obj), การพิมพ์ใน console
repr(np.array([1,2,3]))
'array([1,2,3])'
np.array([1,2,3])
array([1, 2, 3])
  • เป็นทางการ สำหรับนักพัฒนา
  • การแสดงผลแบบ reproducible
  • ใช้เป็น fallback สำหรับ print()
Python เบื้องต้นสำหรับการเขียนโปรแกรมเชิงวัตถุ

การใช้งาน: repr

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

    def __repr__(self):

# Notice the '...' around name return f"Customer('{self.name}', {self.balance})"
cust = Customer("Maryam Azar", 3000) # Will implicitly call __repr__() cust
Customer('Maryam Azar', 3000)
Python เบื้องต้นสำหรับการเขียนโปรแกรมเชิงวัตถุ

การใช้งาน: str

class Customer:
    def __init__(self, name, balance):
        self.name = name
        self.balance = balance
    def __str__(self):
        cust_str = f"""
        Customer:
            name: {self.name}
            balance: {self.balance}
            """
        return cust_str
cust = Customer("Maryam Azar", 3000)

# Will implicitly call __str__()
print(cust)
Customer:
  name: Maryam Azar
  balance: 3000
Python เบื้องต้นสำหรับการเขียนโปรแกรมเชิงวัตถุ

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

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

Preparing Video For Download...