0% found this document useful (0 votes)
6 views29 pages

Python - Lecture 3

Uploaded by

Aya Ismail
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views29 pages

Python - Lecture 3

Uploaded by

Aya Ismail
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Python: The Easy

Way
Lecture
3
Object Oriented
Python
OO
Intro Python

Module
s

Object
Function Oriented
s Level
Modular Level
Procedural
Level
Speghatti
Level

Open Source Department –


OO
Intro Python

height = 175 cm velocity = 200 m/s


weight = 76 kg Propertie brakes = 2
s
walk() stop()
speak( Method move()
) s

ride( BikeObj
)
The ride() method
takes a bike object as
an argument and calls
Man its move() method. Bike
Object Object

Open Source Department –


OO
Python

OOP
Keywords
OOP
Clas Keywords

s
A class is a template definition of an object's properties and
methods.

class Human: Human Class


pass

Open Source Department –


OOP
Object Keywords

An Object is an instance of a
Class.

class Human: Human Class


pass

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

Instance Variable is an object characteristic “unique


to each instance”, such as name.

class Human: Human Class

def init (self, name): name

self.name = name
init ()

man = Human(“Ahmed”)

Name is
Ahmed

Man
Object
Open Source Department –
OOP
Class Variable Keywords

Class Variable is the variable that shared by all


instances.
class Human: Human Class

makeFault = True makeFault = True

def init (self, name): name


self.name = name;

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

print("Human:", Human.faults) Man : 1


Human.faults = 2
print("Man 2:", man2.faults)
print("Human:", Human.faults)
print("Man :", man.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
man.faults = 1 #man-> overridden the class variable
print("Man :", man.faults) Human : 0
print("Man 2:", man2.faults)
Man2 : 2
print("Human:", Human.faults)
“””The line Human.faults = 2 changes the class variable faults Human : 2
value for the class Human and any instance that hasn't
overridden this class variable.””” Man : 1
Human.faults = 2
print("Man 2:", man2.faults)
print("Human:", Human.faults)
print("Man :", man.faults)

Open Source Department –


OOP
Instance Method Keywords

Instance Method is an object capability, such as walk.

It is a function defined inside a class that operate on instances of that class.


They can access and modify instance-specific data. Human Class
class Human:
def init (self, name): name
self.name = name

def speak(self): init ()


print(“My Name is
”+self.name) speak()

man = Human(“Ahmed”)
man.speak()
My Name is Ahmed

Open Source Department –


Class Method OOP
Keywords
Class Method is a method that shared by all instances of the Class
It is a function defined inside a class that are bound to the class rather
than its instances.
They can modify class-level data.
class Human:
faults=0 # Class variable # any changes to faults through a class method will
reflect across all instances.
def init (self, name):
self.name = name # Instance variable
# It is a decorator used to define a class
Human Class
@classmethod
method.
def makeFaults(cls): faults
cls.faults +=1 #Increment the class variable
name
print(cls.faults)
# Calling class method directly using the class name
Human.makeFaults() #Output: 1
init ()
man = Human(“Ahmed”)
# Calling class method using an instance
man.makeFaults() # Output: 2 makeFaults()
Open Source Department –
Static Method OOP
Keywords
Static Method is a normal function that have logic that related to the Class.
They are defined within a class but don't access or modify the class state or
instance state.
They are used for utility functions that perform a task related to the class but
don't need to interact with class or instance variables.
No Access to self or cls.
class Human:
Human Class

def init (self, name): name


self.name = name
@staticmethod
init ()
def measureTemp(temp):
if (temp == 37):
return “Normal” measureTemp()
return “Not Normal”

Human.measureTemp(38) # Not
Normal Open Source Department –
OOP
Static vs Class Methods Keywords

Class Static Method


Method
# cls(Class) is implicity # Static Method is like a
passed to class method like normal function but we put
self(instance) in instance it in the class because it
method. have logic that related to
the class.
# Class Method is related
to the class itself. # We call it Helper

class Human: Method class Human:


@classmethod @staticmethod
def walk(cls): def sleep():
print(“Wal print(“wh
k …”) oa”)

Human.walk() Human.sleep()

Open Source Department –


OO
Python

OOP
Concepts
OOP
Concepts

Inheritanc
e
Inheritanc
Intro e

Human
Class

Employee
Class

Engineer Teacher
Class Class

Open Source Department –


Exampl Inheritanc
e
eclass Human:
def init (self, name):
self.name = name Human Class
def speak(self):
print(“My Name is
”+self.name);
class Employee(Human): #Drived
class
#calling the superclass constructor
def init (self, name, salary):

super(). init (name)


self.salary = salary
def work(self): Employee
print(“I’m working now”); Class
emp = Employee(“Ahmed”, 500)
emp.speak()#output: My name is
Open Source Department –
Inheritanc
Multiple Inheritance e

Python supports Multiple


Inheritance

Human Mammal
Class Class

Employee
Class

Open Source Department –


OOP
Concepts

Polymorphis
m
Polymorphis
Intro m

Poly means "many" and morphism means "forms". Different classes


might define the same method or property.

Eagle Fly() I fly very


fast

Bird Dove Fly() I fly for long


class distances

Fly()

I fly  Penguin Fly() I can’t fly


Open Source Department –


Polymorphis
Method Overriding m

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

print(“My salary is ”+str(self.salary)); Employee


Class
emp = Employee(“Ahmed”, 500)
emp.speak() #My Salary is 500

Open Source Department –


OOP
Concepts

Encapsulati
on
Encapsulatio
intro n

Encapsulation is the packing of data and functions into one


component (for example, a class) and then controlling access to
that component .

It ensures that the internal representation of an object is hidden


from the outside.

Open Source Department –


Encapsulatio
Exampl First method
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

return self. name


# Creating an instance of
Human class
man = Human(“Mahmoud”)
# Attempting to access private attributes
print(man. name) directly
AttributeError:
'Human' object has no
attribute ‘

name’

# Accessing the name


Open Source
through Department –
a public
Second method: use property decorators to create Encapsulatio
@property getter and setter methods that encapsulate data. n

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

You might also like