การโอเวอร์โหลดตัวดำเนินการใน Python

Object-Oriented Programming ใน Python ระดับกลาง

Jake Roach

Data Engineer

การโอเวอร์โหลดตัวดำเนินการเปรียบเทียบ

class Person:
  def __init__(self, name):
      self.name = name

  def __eq__(self, other):
      return self.name == other.name

ปรับแต่งการทำงานของ ==

  • ใช้ magic method __eq__()
  • รับ self และ other
  • คืนค่าเป็น boolean
bryce = Person("Bryce")
orion = Person("Orion")
print(bryce == orion)
False
chuck = Person("Charles Carmichael")
charles = Person("Charles Carmichael")
print(chuck == charles)
True
Object-Oriented Programming ใน Python ระดับกลาง

การบวกออบเจกต์สองตัว

class Team:
  def __init__(self, team_members):
      self.team_members = team_members

# Create two Team objects, attempt to add them
rookies = Team(["Casey", "Emmitt"])
veterans = Team(["Mike", "Chuck"])
dream_team = rookies + veterans
Traceback (most recent call last):
  File "<stdin>", line 8, in <module>
TypeError: unsupported operand type(s) for +: 'Team' and 'Team'
Object-Oriented Programming ใน Python ระดับกลาง

การโอเวอร์โหลดตัวดำเนินการ +

class Team:
  def __init__(self, team_members):
    # team_members is a list of names
    self.team_members = team_members

  def __add__(self, other):
    # Adding Team objects creates a larger Team
    return Team(self.team_members + other.team_members)
  • ใช้ magic method __add__() เพื่อโอเวอร์โหลด +
  • self และ other จะถูกส่งเข้า __add__()
  • สร้างออบเจกต์ Team ใหม่โดยรวม team_members จากออบเจกต์ที่ "บวก" กัน
Object-Oriented Programming ใน Python ระดับกลาง

การบวกออบเจกต์สองตัว

# Create two Team objects
rookies = Team(["Casey", "Emmitt"])
veterans = Team(["Mike", "Chuck"])

# Attempt to add these two Teams together
dream_team = rookies + veterans
print(type(dream_team))
print(dream_team.team_members)
Team
["Casey", "Emmitt", "Mike", "Chuck"]
Object-Oriented Programming ใน Python ระดับกลาง

ใช้ + เพื่อสร้างออบเจกต์ประเภทใหม่

class Team:
  def __init__(self, team_members):
      self.team_members = team_members

class Employee:
  def __init__(self, name, title):
    self.name = name
    self.title = title

  def __add__(self, other):
    # Use the + operator to create a
    # Team with the name of each Employee
    return Team([self.name, other.name])

จะสร้าง Team จากการรวม Employee หลายตัวได้อย่างไร?

  • __add__ ถูกกำหนดไว้ในคลาส Employee
  • สร้างออบเจกต์ Team ใหม่จากรายชื่อของ Employee
  • ผลลัพธ์ของการบวก Employee สองตัวคือ Team เดียว
Object-Oriented Programming ใน Python ระดับกลาง

บวกออบเจกต์สองตัวเพื่อสร้างออบเจกต์ใหม่

# Create two Employee objects
anna = Employee("Anna", "Technical Specialist")
jeff = Employee("Jeffrey", "Musician")

# Now, attempt to add these together to create a team
audio_team = anna + jeff
print(type(audio_team))
print(audio_team.team_members)
Team
["Anna", "Jeffrey"]
Object-Oriented Programming ใน Python ระดับกลาง

การโอเวอร์โหลดตัวดำเนินการอื่น ๆ

ตัวดำเนินการ Magic Method ประเภท
- __sub__ เลขคณิต
!= __ne__ การเปรียบเทียบ
< __lt__ การเปรียบเทียบ
> __gt__ การเปรียบเทียบ
+= __iadd__ การกำหนดค่า
and __and__ ตรรกะ
in __contains__ การตรวจสอบสมาชิก
is __is__ เอกลักษณ์

 

 

 

 

 

 

 

... และอีกมากมาย!

Object-Oriented Programming ใน Python ระดับกลาง

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

Object-Oriented Programming ใน Python ระดับกลาง

Preparing Video For Download...