What is LessonUp
Search
Channels
Log in
Register
‹
Return to search
WK3: Object-Oriented Programming in Python (Session 1)
Welcome
Object-Oriented Programming (OOP) in Python
Week 3
Module Lecturer: Dr Raghav Kovvuri
Email: raghav.kovvuri@ieg.ac.uk
1 / 14
next
Slide 1:
Slide
Programming
Higher Education (degree)
This lesson contains
14 slides
, with
text slides
.
Start lesson
Save
Share
Print lesson
Items in this lesson
Welcome
Object-Oriented Programming (OOP) in Python
Week 3
Module Lecturer: Dr Raghav Kovvuri
Email: raghav.kovvuri@ieg.ac.uk
Slide 1 - Slide
Introduction to OOP (1)
What is OOP?
Definition:
A programming based on the concept of "objects"
Objects:
Contain data (attributes) and code (methods)
Why OOP?
Modularity:
Easier to manage and maintain large codebases
Reusability:
Code can be reused through inheritance
Flexibility:
Easier to add features and modify existing code
Real-world modeling:
Objects can represent real-world entities
Key principles:
Encapsulation:
Bundling of data and methods that operate on that data
Inheritance:
Creating new classes based on existing classes
Polymorphism:
The ability to use a common interface for multiple forms (data types)
Slide 2 - Slide
Class: The Blueprint
Definition:
Class is a template for creating objects. It's like an architect's plan for a house
Key points:
A class defines the structure and behavior that its objects will have.
It specifies what attributes (data) the objects will contain.
It defines what methods (functions) the objects can perform.
A class doesn't occupy memory for the attributes until an object is created.
Real-world analogy:
A class is like a cookie cutter. It defines the shape and features, but it's not the cookie itself.
Slide 3 - Slide
Object: The Instance
An object is created from a class and occupies memory.
Each object has its own set of attributes with unique values.
Objects can perform the methods defined in their class.
Multiple objects can be created from the same class, each being independent
O
bject is a specific instance of a class. A concrete entity created from the class blueprint.
Key points:
Real-world analogy:
If a class is like a cookie cutter, an object is like an actual cookie made from that cutter.
In this example:
toyota
and
honda
are objects (instances) of the Car class.
Each object has its own set of attributes (
make, model, year, is_running
).
We can call methods on these objects (
start_engine(), stop_engine()
).
We can access the attributes of each object independently.
Slide 4 - Slide
Attributes in Python
Instance Variables:
Class Variables:
Unique to each instance of a class
Defined inside methods, typically in the
__init__
method
Accessed using
self.variable_name
Can have different values for different objects
Shared among all instances of a class
Defined outside any method, typically at the top of the class
Accessed using
ClassName.variable_name
or
self.variable_name
Same value for all objects of the class
Slide 5 - Slide
Methods in Python
Instance Methods:
Static Methods:
Class Methods:
Operate on instance data
First parameter is always
self
Can access and modify instance state
Most common type of method
Don't operate on instance or class data
Use
@staticmethod
decorator
Don't have
self
or
cls
parameters
Utility functions that belong to the class namespace
Operate on class data
Use
@classmethod
decorator
First parameter is always
cls
(the class itself)
Can access and modify class state
Often used as alternative constructors
Slide 6 - Slide
The
self
Parameter
self
represents the instance of the class
Used to access variables and methods of the class within the class itself
Automatically passed to instance methods when they are called
By convention, it's named
self
, but you could use any valid variable name (though this is not recommended)
Allows each instance to have its own set of data, distinct from other instances
Slide 7 - Slide
Encapsulation
Benefits:
Bundling of data and methods that operate on that data within a single unit (class)
Information hiding: Restricting direct access to some of an object's components
In Python:
Data protection:
Prevent accidental modification
Flexibility:
Can change internal implementation without affecting other code
Modularity:
Easier to maintain and debug
Public:
No special syntax, can be accessed from anywhere
Protected:
Single underscore prefix
_attribute
(convention, not enforced)
Private:
Double underscore prefix
__attribute
(name mangling)
Slide 8 - Slide
Getters and Setters
Used to control access to class attributes
Allow for validation of data before setting it
Provide a way to change internal implementation without changing the public interface
In Python, often implemented using the
@property
decorator
Slide 9 - Slide
Inheritance
Creating a new class based on an existing class
Terminology:
Superclass/Parent class:
The class being inherited from
Subclass/Child class:
The class that inherits
Allows for code reuse and establishment of a hierarchical relationship between classes
Subclass inherits attributes and methods from the superclass
Subclass can override or extend the inherited attributes and methods
Slide 10 - Slide
Method Overriding and
super()
Method Overriding:
Redefining a method in a subclass that is already defined in the superclass
super():
Used to call methods from the parent class
Slide 11 - Slide
Polymorphism
Ability of different classes to be treated as instances of the same class through inheritance
Allows use of a single interface to represent different underlying forms (data types)
Enables more flexible and reusable code
Slide 12 - Slide
Conclusion and Key Takeaways
Classes and Objects:
Blueprints and instances
Attributes:
Instance variables and class variables
Methods:
Instance methods, static methods, and class methods
Encapsulation:
Data hiding and access control
Inheritance:
Code reuse and hierarchical relationships
Polymorphism:
Flexibility in using different
Slide 13 - Slide
Slide 14 - Slide
More lessons like this
U2L3- Attributes
January 2023
- Lesson with
13 slides
Ap csa
10th Grade
Introduction to Object-Oriented Programming
May 2024
- Lesson with
14 slides
Computing
Upper Secondary (Key Stage 4)
GCSE
Building a Computer Game with Python
October 2023
- Lesson with
26 slides
Mastering Lists in Python
February 2024
- Lesson with
22 slides
Computing
Lower Secondary (Key Stage 3)
Python Programming Fundamentals: Data Types, Functions, and Comments
September 2023
- Lesson with
14 slides
Understanding Data Types in Python
October 2023
- Lesson with
18 slides
WK2: Coding Standards and Hands-on Exercises (Session 2)
September 2024
- Lesson with
11 slides
Programming
Higher Education (degree)
WK2: Procedural Programming and Control Structures in Python (Session 1)
September 2024
- Lesson with
21 slides
Programming
Higher Education (degree)