Lecture Notes on Python Classes and Object-Oriented Programming

Jul 24, 2024

Notes on Python Classes and Object-Oriented Programming

Introduction

  • Series of videos on creating and using classes in Python.
  • Focus on object-oriented concepts.
  • Topics to cover:
    • Creating and instantiating classes
    • Inheritance
    • Class and instance variables
    • Static methods and class methods
    • Other object-oriented topics

Why Use Classes?

  • Classes allow logical grouping of data and functions.
  • Facilitate reuse and buildability of code.
  • Terms:
    • Attributes: Data associated with a class.
    • Methods: Functions associated with a class.

Creating Classes

  1. Example: Employee Class

    • Represents employees in a company.
    • Attributes include name, email, pay, and actions.
    • Creating Class:
      class Employee:
          pass
      
      • Use pass statement to leave class empty for now.
  2. Instances of a Class

    • A class is a blueprint; each employee created is an instance.
    • Example of creating instances:
      employee1 = Employee()
      employee2 = Employee()
      
    • Each instance has a unique memory location.

Instance Variables

  • Unique to each instance.
  • Example of manual assignment of instance variables:
    employee1.first = 'Corey'
    employee1.last = 'Schaefer'
    employee1.email = 'corey.schaefer@company.com'
    employee1.pay = 50000
    
  • Better Approach: Use the __init__ method to initialize instance variables automatically.

Using the __init__ Method (Constructor)

  • Define __init__ to initialize attributes:
    def __init__(self, first, last, pay):
        self.first = first
        self.last = last
        self.pay = pay
        self.email = f'{first}.{last}@company.com'
    
  • Pass attributes when creating an instance:
    employee1 = Employee('Corey', 'Schaefer', 50000)
    employee2 = Employee('Test', 'User', 60000)
    

Adding Methods to a Class

  • Example: Method to display the full name of an employee.
    def full_name(self):
        return f'{self.first} {self.last}'
    
  • Call the method:
    print(employee1.full_name())
    

Common Mistakes

  • Forgetting to include self in method definitions leads to errors:
    • Without self:
      def full_name():
          return f'{self.first} {self.last}'  # Error!
      

Calling Methods from Class vs Instance

  • Calling methods on instances:
    employee1.full_name()
    
  • Calling methods using class name:
    Employee.full_name(employee1)
    
  • Differences in needing to pass in self when using class name.

Summary

  • Learned to create simple classes and the difference between a class and an instance.
  • Initialized class attributes using the __init__ method.
  • Created methods for functionality.
  • Upcoming video will cover class variables vs instance variables.

Engagement and Support

  • Questions can be asked in the comment section.
  • Like and share videos to support.
  • Patreon link available for contributions.