OOP
# Classes ===================== def SetR(self, r): # New method in
subclass
# Programming Paradigms self.R = r
# 1) Imperative: loops, statements,
subroutines def Area(self): # Overridden method:
# 2) Functional: Pure function, Polymorphism
recursions, Higher-order functions print(super().Area() +
# 3) OOP: Object-Oriented Programming str(3.14159*self.R**2))
# Class: Object Definition
class Rectangle(Shape):
class Car: def SetAB(self, a, b): # New Method
# name, color = attributes ... -----------------
def __init__(self, name, self.a = a
color='Pink'): # Constructor Methods self.b = b
self.Name = name
# Method Polymorphism def Area(self): # Overridden Method
self.Color = color -----------------
# all methods all self (instance print(super().Area() + str(self.b
of that object) * self.a))
def SetColor(self, color): C1 = Circle('Circle1')
self.Color = color C1.SetR(3)
C1.Area()
def Print(self):
print('\nCar name: ' + self.Name) Rec1 = Rectangle('Rectangle1')
print('Car color: ' + self.Color Rec1.SetAB(3, 5)
+ '\n') Rec1.Area()
car1 = Car('AOD', 'Black') # Magic Methods (dunders):
car2 = Car('Benz', 'Red') __MethodName__():
# Ex: __init__: Constructor
car1.Print() # Operator Overloading: __add__ for +:
car2.Print()
class Vector:
print('After set new color -------------- def __init__(self, a1, a2, a3):
self.a1 = a1
-------------') self.a2 = a2
car1.SetColor('Blue') self.a3 = a3
car1.Print()
def __add__(self, other): # +
Inheritance ---------------------- return Vector(self.a1 + other.a1,
# share functionality between classes self.a2 + other.a2, self.a3 + other.a3)
def __mul__(self, other): # *
class Shape: # supper class A1 = self.a2*other.a3 -
def __init__(self, name): self.a3*other.a2
self.Name = name A2 = self.a3*other.a1 -
self.a1*other.a3
def Area(self): A3 = self.a1*other.a2 -
return self.Name + " Area is: " self.a2*other.a1
return Vector(A1, A2, A3)
def Perimeter(self):
return self.Name + " Perimeter def Print(self):
is: " print('Vector: ', [self.a1,
self.a2, self.a3])
class Circle(Shape): # subclass
Python course By: Mansour Torabi
OOP
V1 = Vector(1, 2, 5) def __init__(self):
V2 = Vector(-4, 5, -2) pass
V3 = V1 + V2 @staticmethod
V4 = V1 * V2 def Description():
print("This is a class for Cars")
V3.Print()
V4.Print()
Car.Description()
# OOP: Encapsulation -------------
# Data Hiding
# Implementation details should be hidden
# In Python there is not Private Methods
or Attributes
# But there are some convention
# Weakly Private: single underscore:
[stay away even if you're not technically
prevented from doing so]
# Private: double underscore
class Persion:
def __init__(self, n, a1='Tom',
a2='BigFish'):
self.name = n
self._alias1 = a1
self.__alias2 = a2
def ShowName(self):
print(self.name)
self._ShowAlias()
def _ShowAlias(self):
print(self.__AliasFormat())
def __AliasFormat(self):
return "Alias Name1: " +
self._alias1 +'\nAlias Name2: ' +
self.__alias2 + '\n'
P1 = Persion("John")
P1.ShowName()
P1._ShowAlias() # can be access but
signal not to do it
try:
P1.__AliasFormat() # Can Not Access
except:
print("Acess Error")
print('------------------')
# Static Methods
class Car:
Python course By: Mansour Torabi