Sovraccaricare gli operatori di Python

Programmazione a oggetti intermedia in Python

Jake Roach

Data Engineer

Sovraccaricare gli operatori di confronto

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

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

Personalizza la funzionalità di ==

  • Usa il metodo magico __eq__()
  • Prende self e other
  • Restituisce un booleano
bryce = Person("Bryce")
orion = Person("Orion")
print(bryce == orion)
False
chuck = Person("Charles Carmichael")
charles = Person("Charles Carmichael")
print(chuck == charles)
True
Programmazione a oggetti intermedia in Python

Sommare due oggetti

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'
Programmazione a oggetti intermedia in Python

Sovraccaricare l'operatore +

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)
  • Usa il metodo magico __add__() per sovraccaricare +
  • self e other sono passati a __add__()
  • Crea un nuovo oggetto Team usando i team_members degli oggetti "sommati"
Programmazione a oggetti intermedia in Python

Sommare due oggetti

# 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"]
Programmazione a oggetti intermedia in Python

Usare + per creare un nuovo tipo di oggetto

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

E se volessimo creare un Team combinando oggetti Employee?

  • __add__ è implementato nella classe Employee
  • Crea un nuovo Team costruendo una lista di nomi degli Employee
  • Il risultato di sommare due Employee è un unico Team
Programmazione a oggetti intermedia in Python

Somma due oggetti per creare un nuovo oggetto

# 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"]
Programmazione a oggetti intermedia in Python

Sovraccaricare altri operatori

Operatore Metodo magico Tipo di operatore
- __sub__ Aritmetico
!= __ne__ Confronto
< __lt__ Confronto
> __gt__ Confronto
+= __iadd__ Assegnazione
and __and__ Logico
in __contains__ Appartenenza
is __is__ Identità

 

 

 

 

 

 

 

... E molti altri!

Programmazione a oggetti intermedia in Python

Esercitiamoci!

Programmazione a oggetti intermedia in Python

Preparing Video For Download...