Python - Lecture 3
Python - Lecture 3
Way
Lecture
3
Object Oriented
Python
OO
Intro Python
Module
s
Object
Function Oriented
s Level
Modular Level
Procedural
Level
Speghatti
Level
ride( BikeObj
)
The ride() method
takes a bike object as
an argument and calls
Man its move() method. Bike
Object Object
OOP
Keywords
OOP
Clas Keywords
s
A class is a template definition of an object's properties and
methods.
An Object is an instance of a
Class.
man = Human()
Man
Object
Open Source Department –
OOP
Constructor Keywords
Constructor is a method called at the moment an object is
instantiated.
class Human:
def init (self):
“”” self is a special keyword used in Human Class
class methods to represent the
instance of the class.
It is used to access instance
variables and methods from within
the class.
When you define methods for a class,
you need to explicitly specify init ()
self as the first parameter to
access the instance variables and
methods.”””
print(“Hi there”)
man = Human()
Output:
Hi there
Man
Object
Open Source Department –
OOP
Instance Variable Keywords
self.name = name
init ()
man = Human(“Ahmed”)
Name is
Ahmed
Man
Object
Open Source Department –
OOP
Class Variable Keywords
man = Human(“Ahmed”)
init ()
man2 = Human(“Mohamed”)
Name is Name is
Ahmed Mohamed
He makes He makes
faults faults
Open Source Department –
OOP
Class Variable Keywords
class Human:
faults = 0 Output:
def init (self, name):
self.name = name;
Man : 1
man = Human(“Ahmed”)
man2 = Human(“Mohamed”) Man2 : 0
“”” When man.faults = 1 is executed, a new instance
variable faults is created for the man object, shadowing the
class variable faults.”””
Human : 0
man.faults = 1
Man2 : 2
print("Man :", man.faults)
print("Man 2:", man2.faults) Human : 2
man = Human(“Ahmed”)
man.speak()
My Name is Ahmed
Human.measureTemp(38) # Not
Normal Open Source Department –
OOP
Static vs Class Methods Keywords
Human.walk() Human.sleep()
OOP
Concepts
OOP
Concepts
Inheritanc
e
Inheritanc
Intro e
Human
Class
Employee
Class
Engineer Teacher
Class Class
Human Mammal
Class Class
Employee
Class
Polymorphis
m
Polymorphis
Intro m
Fly()
class Human:
def init (self, name):
self.name = name Human Class
def speak(self):
print(“My Name is ”+self.name);
class Employee(Human):
def init (self, name, salary): super().
init (name)
self.salary = salary
def speak(self):
# This method overrides the `speak` method in the `Human` class
Encapsulati
on
Encapsulatio
intro n
e
class Human:
def init (self, name):
self. name = name # private attribute: “cannot be accessed directly from
outside the class.”
def getName(self): # public method
name’
class Human:
def init (self, age):
self.__age = age
# @property decorator makes the age method act as a getter for the _age attribute.
@property
def age(self):
return self. age # Return the age attribute
@age.setter
def age(self, age):
if age > 0:
self. age = age # Set the age if it's greater than 0
if age <= 0:
self. age = 0 # Set the age to 0 if the given age is not valid (<= 0)
# Creating an instance of Human with age 23
man = Human(23)
print(man.age)# calls the getter method associated with the age property. #
23
man.age = -25 # This invokes the setter method and trying to set
a negative age
print(man.age) # the getter method returns 0. #0
Open Source Department –
Thank
You