Overloading Python Operators

Intermediate Object-Oriented Programming in Python

Jake Roach

Data Engineer

Overloading comparison operators

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

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

Customize the functionality of ==

  • Use the __eq__() magic method
  • Takes self and other
  • Returns a boolean value
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

Adding two objects

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

Overloading the + operator

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)
  • Use the __add__() magic method to overload +
  • self and other are passed to __add__()
  • Create a new Team object using the team_members from objects being "added"
Intermediate Object-Oriented Programming in Python

Adding two objects

# 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

Using + to create a new type of object

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

What if we want to create a Team by combining Employee objects?

  • __add__ is implemented in the Employee class
  • Creates a new object of Team by creating a list of Employee names
  • The result of adding two Employee objects is a single Team
Intermediate Object-Oriented Programming in Python

Add two objects to create a new object

# 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

Overloading other operators

Operator Magic Method Operator Type
- __sub__ Arithmetic
!= __ne__ Comparison
< __lt__ Comparison
> __gt__ Comparison
+= __iadd__ Assignment
and __and__ Logical
in __contains__ Membership
is __is__ Identity

 

 

 

 

 

 

 

... And tons more!

Intermediate Object-Oriented Programming in Python

Let's practice!

Intermediate Object-Oriented Programming in Python

Preparing Video For Download...