Wat is LessonUp
Zoeken
Kanalen
Inloggen
Registreren
‹
Terug naar zoeken
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
volgende
Slide 1:
Tekstslide
Programming
Higher Education (degree)
In deze les zitten
14 slides
, met
tekstslides
.
Start les
Bewaar
Deel
Printen
Onderdelen in deze les
Welcome
Object-Oriented Programming (OOP) in Python
Week 3
Module Lecturer: Dr Raghav Kovvuri
Email: raghav.kovvuri@ieg.ac.uk
Slide 1 - Tekstslide
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 - Tekstslide
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 - Tekstslide
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 - Tekstslide
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 - Tekstslide
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 - Tekstslide
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 - Tekstslide
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 - Tekstslide
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 - Tekstslide
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 - Tekstslide
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 - Tekstslide
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 - Tekstslide
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 - Tekstslide
Slide 14 - Tekstslide
Meer lessen zoals deze
Introduction to Object-Oriented Programming
Mei 2024
- Les met
14 slides
Computing
Upper Secondary (Key Stage 4)
GCSE
U2L3- Attributes
Januari 2023
- Les met
13 slides
Ap csa
10th Grade
Building a Computer Game with Python
Oktober 2023
- Les met
26 slides
Understanding Data Types in Python
Oktober 2023
- Les met
18 slides
Python Programming Fundamentals: Data Types, Functions, and Comments
September 2023
- Les met
14 slides
Mastering Lists in Python
Februari 2024
- Les met
22 slides
Computing
Lower Secondary (Key Stage 3)
WK2: Coding Standards and Hands-on Exercises (Session 2)
September 2024
- Les met
11 slides
Programming
Higher Education (degree)
WK2: Procedural Programming and Control Structures in Python (Session 1)
September 2024
- Les met
21 slides
Programming
Higher Education (degree)