Python - Lecture 3
Python - Lecture 3
Lecture 3
Object Oriented Python
OO Python
Intro
Modules
Object Oriented
Functions Level
Modular Level
Procedural
Level
Speghatti Level
walk() stop()
speak() Methods move()
ride( BikeObj )
OOP Keywords
OOP Keywords
Class
man = Human()
Man Object
def __init__(self):
print(“Hi there”) __init__()
man = Human()
Output:
Hi there
Man Object
self.name = name
__init__()
man = Human(“Ahmed”)
Name is Ahmed
Man Object
man = Human(“Ahmed”)
man2 = Human(“Mohamed”) __init__()
My Name is Ahmed
@classmethod
__init__()
def makeFaults(cls):
cls.faults +=1
makeFaults()
print(cls.faults)
Human.makeFaults() #1
man = Human(“Ahmed”)
man.makeFaults() #2
__init__()
@staticmethod
def measureTemp(temp):
if (temp == 37): measureTemp()
return “Normal”
return “Not Normal”
Human.walk() Human.sleep()
OOP Concepts
OOP Concepts
Inheritance
Inheritance
Intro
Human
Class
Employee
Class
Engineer Teacher
Class Class
Report**:
Human Mammal
1- How super Function handle Multiple Inheritance.
Class Class
2- If Human and Mammal Have the same method
Polymorphism
Polymorphism
Intro
Poly means "many" and morphism means "forms". Different classes might define
the same method or property.
Fly()
Report**:
Can we do overloading in Python ?
Encapsulation
Encapsulation
intro
Encapsulation is the packing of data and functions into one component (for
example, a class) and then controlling access to that component .
getName()
Class
amigos
class Human:
def __init__(self, name):
self.__name = name
def getName(self):
return self.__name
man = Human(“Mahmoud”)
print(man.__name)
AttributeError: 'Human' object has no attribute ‘__name’
print(man.getName())
#output: Mahmoud
@property
def age(self):
return self.__age
@age.setter
def age(self, age):
if age > 0:
self.__age = age
if age <= 0:
self.__age = 0
man = Human(23)
print(man.age) # 23
man.age = -25
print(man.age) # 0
class Human:
def __init__(self, name):
self.name = name
def __str__(self):
man = Human(“Ahmed”)
print(man)
#output: <__main__.Human object at 0x000000FD81804400>
print(man)
#output: Hi, I’m Human and my name is Ahmed
class Human:
def __init__(self, name):
self.name = name
def __call__(self):
man = Human(“Ahmed”)
man()
#output: TypeError: 'Employee' object is not callable
man()
#output: You called me !
class Animal:
def __init__(self, legs):
self.legs = legs
def __len__(self):
return self.legs
dog = Animal(4)
len(dog)
#output: TypeError: 'Employee' object has no len()
len(dog)
#output: 4
lambda input:output
Example
Example
Example
map(function, sequence)
Map function are used to make iterables from apply the given function on every
item in the given sequence
Example
filter(condfunction, sequence)
Filter function are used to make iterables from filter each item in the given
sequence by the given function
Example