12 - OOP in Python
12 - OOP in Python
Computing/Information System
• Course Page
UOG 6: https://lms.lnbti.lk/course/view.php?id=851
UOG 11: https://lms.lnbti.lk/course/view.php?id=1306
• References available for the course
w3schools Python - https://www.w3schools.com/Python/
Real Python - https://realpython.com/
• Object-oriented programming
is a programming paradigm.
• It provides a means of
structuring programs so that
properties and behaviors are
bundled into individual
objects.
Object
Note:
Each object is an “instance” of
that “class” of object.
Each instance has its own
values for its attributes.
Ex: Different students have
different names
Class
Person
Attributes
• A class is a collection of Name
objects. Date of Birth
Address
• A class contains the
blueprints or the prototype Behaviors
from which the objects are
Walk
being created.
Eat
• It is a logical entity that Sleep
contains some attributes and
methods.
Class Object
(Blueprint of the object) (Instance of the class)
Importance of the classes
Here,
• class name is Student.
• university is an attribute that belongs to the
class.
Empty Class
Class Student
University =
LNBTI
Instance variables Name
Address
self
• Class methods must have an extra first parameter in the method
definition.
• We do not give a value for this parameter when we call the method,
Python provides it.
• If we have a method that takes no arguments, then we still have to have
one argument.
__init__ method
• The __init__ method is similar to constructors in C++ and Java.
• It is run as soon as an object of a class is instantiated.
• The method is useful to do any initialization you want to do with your
object.
Creating
Objects
Class Student
University =
LNBTI
Name
Address
Object S1
University = LNBTI
Name = Kamal
Address = Maharagama
Destroy Objects
• In Python, you don't typically need to manually destroy objects because Python
has a built-in garbage collector that automatically manages memory.
• However, you can remove references to an object, which allows the garbage
collector to clean it up when it's no longer needed.
• Two methods:
single unit.
Class
__init__ method
Inheritance
A way to create a new class that inherits attributes and methods
OOP Concepts
Child class
__init__ method
in Child class
Types
of
Inheritanc
e
Polymorphism
Having many forms.
OOP Concepts
Method
Overriding Child class
Overriding
Child class
Overriding
Method Overloading
Method overloading in Python can be done using the following methods.
• Default Arguments:
• Provide default values for parameters.
• Variable-Length Arguments:
• Use *args and **kwargs to accept a flexible number of arguments.
• Type Checking:
• Implement conditional logic based on argument types or counts.
• singledispatch:
• Use function dispatching based on argument types.
• Method Wrappers:
• Create methods that handle different argument scenarios and dispatch to
specific implementations.
Abstraction
OOP Concepts
Hiding the internal implementations of a process or
method from the user.
• In this way, the user knows what he is doing but not how the work is
being done.
• Ex: Car
Abstract methods are methods that
must be implemented by any
concrete subclass of the abstract
base class. Parent class
• ABC Class:
• ABC stands for Abstract Base
Class.
• It is a base class provided by
the abc module that you can
inherit from to create abstract Child class
base classes.
• @abstractmethod Decorator:
• Used to mark methods in an
abstract base class as
abstract.
END