Python Classes 1
Python Classes 1
Classes
Agenda
1. Overview
4. Inheritance
5. Importing Classes
1- Overview
• Programming Paradigms:
• Procedural Programming:
• Code written as a sequence of steps.
• Well-suited for data analysis and scripts.
• Object-Oriented Programming (OOP):
• Code written as interactions between objects.
• Ideal for building frameworks and tools.
• Promotes maintainable and reusable code!
• Inheritance: Creating new classes that inherit functionality from existing ones (code
reusability).
• Benefits of Classes:
• Introducing Classes: We define a class using the class keyword followed by the class
name (e.g., Customer).
• The Customer Class: This class represents a generic customer, not a specific one in
our database.
• Defining Attributes: We specify the information each customer instance will hold, like
email and phone_number.
• class definitions don't require parentheses when we're creating the class from scratch.
• Introducing Classes: We define a class using the class keyword followed by the class
name (e.g., Customer).
• The Customer Class: This class represents a generic customer, not a specific one in
our database.
• Defining Attributes: We specify the information each customer instance will hold, like
email and phone_number.
Example:
class Customer:
"""A simple representation of a customer"""
# ... (rest of the class definition will go here)
• class definitions don't require parentheses when we're creating the class from scratch.
• Constructor:
• The __init__() method acts as the constructor for a class. It's called automatically
whenever we create a new object (instance) from the class.
• Think of it as the assembly line where our customer objects are built with their initial
attributes.
• Example:
class Customer:
"""A simple representation of a customer"""
def __init__(self, email, phone_number):
"""Initialize email and phone number attributes."""
self.email = email
self.phone_number = phone_number
# ... (rest of the class definition)
2- Creating and Using a Class
The __init__() Method - Customer Initialization
• The first argument within the __init__() method definition is always self. This argument
refers to the current object being created.
• It's like a special variable that holds a reference to the specific customer instance we're
working with.
• Inside the __init__() method, we use assignment statements to define the object's
attributes. These attributes store the data specific to each customer.
• In our example, we assign the email and phone_number arguments passed to the method to
the corresponding attributes (self.email and self.phone_number).
2- Creating and Using a Class
The __init__() Method - Customer Initialization
• Inside the __init__() method, we use assignment statements to define the object's
attributes. These attributes store the data specific to each customer.
• In our example, we assign the email and phone_number arguments passed to the method
to the corresponding attributes (self.email and self.phone_number).
• Once defined within __init__(), we can access and manipulate the customer's attributes
throughout the class using the self keyword.
• For example, self.email refers to the email address associated with the specific customer
object.
2- Creating and Using a Class
Instantiation:
• Imagine using a cookie cutter (class) to create individual cookies (objects) from cookie dough.
• arguments are the values passed to the class's __init__() method (if any).
Define the “place_order” and “cancel_order” methods then create two objects
class Customer:
"""A simple representation of a customer"""
def __init__(self, email, phone_number):
"""Initialize email and phone number attributes."""
self.email = email
self.phone_number = phone_number
def place_order(self):
"""Simulates a customer placing an order"""
print(f"Customer {self.email} placed an order!")
def cancel_order(self):
"""Simulates a customer canceling an order"""
print(f"Customer {self.email} cancelled an order.”)