Qu'est-ce que LessonUp
Rechercher
Canaux
Connectez-vous
S'inscrire
‹
Revenir à la recherche
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
suivant
Slide 1:
Diapositive
Programming
Higher Education (degree)
Cette leçon contient
14 diapositives
, avec
diapositives de texte
.
Commencer la leçon
Partager
Imprimer la leçon
Éléments de cette leçon
Welcome
Object-Oriented Programming (OOP) in Python
Week 3
Module Lecturer: Dr Raghav Kovvuri
Email: raghav.kovvuri@ieg.ac.uk
Slide 1 - Diapositive
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 - Diapositive
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 - Diapositive
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 - Diapositive
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 - Diapositive
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 - Diapositive
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 - Diapositive
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 - Diapositive
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 - Diapositive
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 - Diapositive
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 - Diapositive
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 - Diapositive
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 - Diapositive
Slide 14 - Diapositive
Plus de leçons comme celle-ci
Introduction to Object-Oriented Programming
Mai 2024
- Leçon avec
14 diapositives
Computing
Upper Secondary (Key Stage 4)
GCSE
U2L3- Attributes
Janvier 2023
- Leçon avec
13 diapositives
Ap csa
10th Grade
Building a Computer Game with Python
Octobre 2023
- Leçon avec
26 diapositives
Understanding Data Types in Python
Octobre 2023
- Leçon avec
18 diapositives
Python Programming Fundamentals: Data Types, Functions, and Comments
Septembre 2023
- Leçon avec
14 diapositives
Mastering Lists in Python
Février 2024
- Leçon avec
22 diapositives
Computing
Lower Secondary (Key Stage 3)
WK2: Coding Standards and Hands-on Exercises (Session 2)
Septembre 2024
- Leçon avec
11 diapositives
Programming
Higher Education (degree)
WK2: Coding Standards and Hands-on Exercises (Session 2)
Septembre 2024
- Leçon avec
11 diapositives
Programming
Higher Education (degree)