Introduction to Classes in Python

Jul 3, 2024

Introduction to Classes in Python

Overview

  • Learn about creating and using classes in Python.
  • Topics will cover:
    • Basics of creating/instantiating classes
    • Inheritance
    • Class and instance variables
    • Static and class methods
    • Other advanced topics

Why Use Classes?

  • Allow logical grouping of data (attributes) and functions (methods).
  • Reusable and easy to build upon.

Terms to Know

  • Attributes: Data associated with a class.
  • Methods: Functions associated with a class.

Creating a Simple Class

Blueprint for Instances

  • Example application: Represent employees in Python code.
  • Each employee can have specific attributes (e.g., name, email, pay) and methods (actions).
  • Define a class:
    class Employee:
        pass
    

Instantiating Classes

  • Instance: A unique object created using the class blueprint.
    employee_1 = Employee()
    employee_2 = Employee()
    
  • Each instance is a unique object in memory.

Instance Variables

  • Contain data unique to each instance.
  • Set manually:
    employee_1.first_name = "Corey"
    employee_1.last_name = "Schaefer"
    employee_1.email = "corey.schaefer@company.com"
    employee_1.pay = 50000
    
  • Manually setting many attributes for each instance is cumbersome and error-prone.

Using the __init__ Method

Purpose

  • Initialize instance variables when the instance is created.
  • Acts like a constructor from other programming languages.

Example

  • Define __init__ method:
    class Employee:
        def __init__(self, first, last, pay):
            self.first = first
            self.last = last
            self.pay = pay
            self.email = f"{first}.{last}@company.com"
    
  • Automatically called when an instance is created:
    employee_1 = Employee("Corey", "Schaefer", 50000)
    employee_2 = Employee("Test", "User", 60000)
    

Creating Methods

Example: Full Name Method

  • Define a method within the class:
    class Employee:
        def __init__(self, first, last, pay):
            self.first = first
            self.last = last
            self.pay = pay
            self.email = f"{first}.{last}@company.com"
    
        def full_name(self):
            return f"{self.first} {self.last}"
    
  • Use method with an instance:
    print(employee_1.full_name())  # Output: Corey Schaefer
    print(employee_2.full_name())  # Output: Test User
    ``
    

Calling Method via Class

  • Alternative way to call method using class name:
    print(Employee.full_name(employee_1))  # Output: Corey Schaefer
    
  • Must pass instance manually.

Common Mistake: Forgetting self

  • Omitting self in method definitions can lead to errors:
    class Employee:
        def full_name():  # Missing `self`
            return f"{self.first} {self.last}"
    
  • Always include self as the first argument in method definitions.

Conclusion

  • Basics of creating classes and instances.
  • Initializing attributes using __init__.
  • Creating and using methods.

Next Steps

  • Future videos will cover class variables and more advanced topics.

Support

  • Like, share, and subscribe.
  • Support through Patreon.