0% found this document useful (0 votes)
14 views13 pages

Python Classes 1

The document discusses Python classes, including defining a class, creating class methods and objects, and using object attributes and methods. It provides examples of defining a Customer class with initialization and customer action methods like place_order() and cancel_order(), and creating Customer objects to call the methods.

Uploaded by

BasSem Reda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views13 pages

Python Classes 1

The document discusses Python classes, including defining a class, creating class methods and objects, and using object attributes and methods. It provides examples of defining a Customer class with initialization and customer action methods like place_order() and cancel_order(), and creating Customer objects to call the methods.

Uploaded by

BasSem Reda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Python Classes

Classes
Agenda

1. Overview

2. Creating and Using a Class

3. Working with Classes and Instances

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!

• OOP (Object-Oriented Programming): A powerful approach to writing software.


• Classes: Represent real-world things and define general behavior for a category of objects.
• Objects: Instances of classes, each with the general behavior and the ability to hold unique traits.
• Instantiation: Creating an object from a class.
1- Overview
• Key Concepts in Classes:

• Specifying information to be stored (data).

• Defining actions that can be performed on the data (methods).

• Inheritance: Creating new classes that inherit functionality from existing ones (code
reusability).

• Benefits of Classes:

• Modeling real-world scenarios effectively.

• Improved code organization and readability.

• Easier collaboration with other programmers.

• Encourages logical thinking for problem-solving.


2- Creating and Using a Class
• Defining a Class:

• 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.

• By convention, capitalized names refer to classes in Python.

• class definitions don't require parentheses when we're creating the class from scratch.

• This defines the blueprint for our customer objects.


2- Creating and Using a Class
• Defining a Class:

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

• By convention, capitalized names refer to classes in Python.

• class definitions don't require parentheses when we're creating the class from scratch.

• This defines the blueprint for our customer objects.


2- Creating and Using a Class
The __init__() Method - Customer Initialization

• 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

• self as the First Argument:

• 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.

• Defining Attributes by Assignment:

• 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

• Defining Attributes by Assignment:

• 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).

• Referencing Attributes in Class:

• 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:

• The process of creating a specific object (instance) from a class blueprint.

• Imagine using a cookie cutter (class) to create individual cookies (objects) from cookie dough.

• Syntax: object_name = ClassName(arguments)

• object_name is a chosen name for the new customer object.

• ClassName is the name of the class (e.g., Customer).

• arguments are the values passed to the class's __init__() method (if any).

Example: customer1 = Customer("[email protected]", "555-123-4567")


2- Creating and Using a Class
Class Methods: “Customer Actions”

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.”)

# Create customer objects


customer1 = Customer("[email protected]", "555-123-4567")
customer2 = Customer("[email protected]", "123-456-7890")
2- Creating and Using a Class
place_order() Method:

• This method defines an action a customer can take - placing an order.


• It doesn't take any arguments (def place_order(self):).
• Inside the method, it uses f-strings (formatted string literals) to print a message that
includes the customer's email retrieved using self.email.
cancel_order() Method:
• This method defines another action - canceling an order.
• It follows the same structure as place_order(), but the message reflects canceling an order.
Call the Methods:
# Call methods on customer objects
customer1.place_order()
customer2.cancel_order()
When you run this code, you'll see the following output:
Customer [email protected] placed an order!
Customer [email protected] cancelled an order.

You might also like