Přetěžování operátorů Pythonu

Intermediate Object-Oriented Programming in Python

Jake Roach

Data Engineer

Přetěžování porovnávacích operátorů

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

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

Přizpůsobení funkce ==

  • Použijte magickou metodu __eq__()
  • Přijímá self a other
  • Vrací booleovskou hodnotu
bryce = Person("Bryce")
orion = Person("Orion")
print(bryce == orion)
False
chuck = Person("Charles Carmichael")
charles = Person("Charles Carmichael")
print(chuck == charles)
True
Intermediate Object-Oriented Programming in Python

Sčítání dvou objektů

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'
Intermediate Object-Oriented Programming in Python

Přetěžování operátoru +

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)
  • K přetížení + použijte magickou metodu __add__()
  • Do __add__() se předávají self a other
  • Vytvoří nový objekt Team z team_members sčítaných objektů
Intermediate Object-Oriented Programming in Python

Sčítání dvou objektů

# 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"]
Intermediate Object-Oriented Programming in Python

Použití + k vytvoření nového typu objektu

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])

Co kdybychom chtěli vytvořit Team kombinací objektů Employee?

  • __add__ je implementována ve třídě Employee
  • Vytvoří nový objekt Team ze seznamu jmen Employee
  • Výsledkem sečtení dvou objektů Employee je jeden Team
Intermediate Object-Oriented Programming in Python

Sečtení dvou objektů pro vytvoření nového objektu

# 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"]
Intermediate Object-Oriented Programming in Python

Přetěžování dalších operátorů

Operátor Magická metoda Typ operátoru
- __sub__ Aritmetický
!= __ne__ Porovnávací
< __lt__ Porovnávací
> __gt__ Porovnávací
+= __iadd__ Přiřazovací
and __and__ Logický
in __contains__ Členství
is __is__ Identita

 

 

 

 

 

 

 

... A mnoho dalších!

Intermediate Object-Oriented Programming in Python

Pojďme cvičit!

Intermediate Object-Oriented Programming in Python

Preparing Video For Download...