Fundamentals of Object-Oriented Programming

Intermediate Object-Oriented Programming in Python

Jake Roach

Data Engineer

Defining a class

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

# This invokes a call to __init__
john = Person("John Casey", 38)
  • Define a class with the class keyword
  • __init__ is known as a constructor, and is called when a new class object is created
  • self refers to the current instance of a class
Intermediate Object-Oriented Programming in Python

Instance attributes

class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age
# Create an instance of Person
sarah = Person("Sarah Walker", 31)
sarah.age  # Retrieve the age instance attribute
  • Associated with an object of a class
  • Can be set and retrieved with the syntax self.<attribute-name>
  • Accessed via <object-name>.<attribute-name>
Intermediate Object-Oriented Programming in Python

Class attributes

  • Tied to the class itself
  • Can be retrieved without an object of the class
  • Store data that should be the same for all class objects
class Person:
  residence = "Planet Earth"
  ...
# Accessed without an instance
print(Person.residence)
Planet Earth
Intermediate Object-Oriented Programming in Python

Instance methods

class Person:
  ...
  def introduce(self):
      print(f"Hello, my name is {self.name}")
chuck = Person("Chuck", 32)
chuck.introduce()  # Called on a Person object

$$

  • Require an object of the class to exist to be called
  • Take the self keyword as the first parameter
Intermediate Object-Oriented Programming in Python

Class methods

class Person:
  @classmethod
  def wake_up(cls):
      print("Time to start your day!")
# Calling a class method
Person.wake_up()

$$

  • Decorated with @classmethod
  • Do not require a class object to be called
Intermediate Object-Oriented Programming in Python

Inheritance

Inheritance allows for code to be reused between classes

  • The child class (Employee) inherits all the functionality of the parent class (Person)
  • "is-a" relationship
  • Can implement additional functionality
    • Attributes
    • Methods
class Person:
  def __init__(self, name, age):
    self.name
    self.age = age

  def introduce(self):
      print(f"Hello, my name is {self.name}")
class Employee(Person):
  def __init__(self, name, age, title):
    Person.__init__(self, name, age)
    self.title = title

  def change_position(self, new_title):
      self.title = new_title
Intermediate Object-Oriented Programming in Python

Inheritance

lester = Employee("Lester", 26, "Technician")
lester.introduce()  # Inherited from Person
print(lester.title)
Hello, my name is Lester
Technician
lester.change_position("Cashier")
print(lester.title)
Cashier
Intermediate Object-Oriented Programming in Python

super()

class Employee(Person):
  def __init__(self, name, age, title):
    # Uses name of the parent class
    Person.__init__(self, name, age)

    self.title = title
  ...
  • Class name, __init__()

Using super()

class Employee(Person):
  def __init__(self, name, age, title):
    # super(), instead of class name
    super().__init__(name, age)

    self.title = title
  ...
  • super(), __init__()
  • self does not need to be passed
Intermediate Object-Oriented Programming in Python

Overriding

Child implements a method that was inherited from parent in a new way

class Employee(Person):
  ...
  def introduce(self):
      print(f"""My name is {self.name},
        I am a {self.title}""")
lester = Employee("Lester", 26, "Technician")
lester.introduce()
My name is Lester, I am a Technician
Intermediate Object-Oriented Programming in Python

Overloading

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

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

$$

  • Customize the behavior of Python operators for a class
  • __eq__() is used to overload ==

$$

$$

$$

$$

# Compare two Person objects
chuck = Person("Charles Carmichael")
charles = Person("Charles Carmichael")
print(chuck == charles)
True
Intermediate Object-Oriented Programming in Python

Let's practice!

Intermediate Object-Oriented Programming in Python

Preparing Video For Download...