Python:
OOPS important Concepts
static and class method
'''static method :
1.cannot access any properties
2. Can have no parameters at all.
Class method:
1) Similar to static method but can use class properties .
2) Must have a reference to a class object as the first parameter’’’
class stat_test():
static and class method
'''static method :
1.cannot access any properties
2. Can have no parameters at all.
Class method:
1) Similar to static method but can use class properties .
2) Must have a reference to a class object as the first parameter’’’
class stat_test():
var="private" #properties
def method(self):
return ("normal method")
@staticmethod
def static_method(self):
return(self.var)
@classmethod
def class_method(self):
return(self.var)
print(stat_test().method())
#print(stat_test.static_method())
#error
print(stat_test.class_method())
print("#========#Access Modifiers(Encapsulation)
DUNDEE =======")
private: "__" at the beginning and at most “_” at end
protected: "_" at the beginning and at most “_” at end (Convention only)
Restricted": "__a__" at beginning and at end
E.g.: print ("adasdsa".__len__()) #__name__ = inbuilt functions
'''
class a:
private: "__" at the beginning and at most “_” at end
protected: "_" at the beginning and at most “_” at end (Convention only)
Restricted": "__a__" at beginning and at end
E.g.: print ("adasdsa".__len__()) #__name__ = inbuilt functions
'''
class a:
pubv="pub"
_prov="pro"
__priv="priv"
def __init__(self):
pass
def pubm(self):
return(self.pubv)
def _prom(self):
return(self._prov)
def __prim(self):
return(self.__priv)
class b(a):
def __init__(self):
pass
def priv(self):
return(self.__priv)
def prov(self):
return(self._prov)
def prim(self):
return(self.__privm())
def prom(self):
return(self._prom())
def pubm(self):
return(super().pubm())
A=a()
print(A.pubv,A._prov)
#print(A.__priv) #
Error
print(A.pubm(),A._prom())
#print(A.__prim())
# Error
B=b()
#print(B.priv()) #
Error
print(B.prov())
#print(B.prim()) #
error
print(B.prom())
print("#====Constructor/Method Overriding and Inheritance===")
#Note : Overloading does not exist in python , only overriding exist
print("#====Constructor/Method Overriding and Inheritance===")
#Note : Overloading does not exist in python , only overriding exist
class Bird():
name=None
def __init__(self,name):
self.name=name
def bird_name(self):
return(self.name)
def fly(self):
return("True")
class
non_fly(Bird):
swim=None
def __init__(self,name,swim): #Constr
overriding
super().__init__(name) #call
parent constr
# Bird.__init__(self, name)
self.swim=swim
def swim(self):
print(self.bird_name(),self.swim) #Method inheritance
def fly(self): #Method Overrdiing
return("false")
peng=non_fly("peng",True)
print(peng.bird_name(),peng.fly(),peng.swim)#inheritance
#O/P: peng false True
print("#==========#polymorphism===================")
#Polymorphism (Duck Typing)
'''In a duck-typed language (if it looks like a duck and quacks like a duck, it's a duck)
If the object have the methods, then execute else it raises a run-time error.’’’
class Duck():
def quack(self):
print ("Duck Quack")
class Mallard():
def quack(self):
print ("Mallard Quack")
#Polymorphism
for i in [Duck(), Mallard()]:
i.quack()
print("#=============Abstract Class ==========")
#O/P: peng false True
print("#==========#polymorphism===================")
#Polymorphism (Duck Typing)
'''In a duck-typed language (if it looks like a duck and quacks like a duck, it's a duck)
If the object have the methods, then execute else it raises a run-time error.’’’
class Duck():
def quack(self):
print ("Duck Quack")
class Mallard():
def quack(self):
print ("Mallard Quack")
#Polymorphism
for i in [Duck(), Mallard()]:
i.quack()
print("#=============Abstract Class ==========")
#( At
least 1 method is abstract,entire class = abstract class)
from abc import ABC, abstractmethod
class Abstract(ABC):
@abstractmethod
def foo(self):
pass
#a=Abstract()#uncomment below to raise exception
print("#=========Diamond problem============")
class Duck():
def quack(self):
print ("Duck Quack")
class Mallard():
def quack(self):
print ("Mallard Quack")
from abc import ABC, abstractmethod
class Abstract(ABC):
@abstractmethod
def foo(self):
pass
#a=Abstract()#uncomment below to raise exception
print("#=========Diamond problem============")
class Duck():
def quack(self):
print ("Duck Quack")
class Mallard():
def quack(self):
print ("Mallard Quack")
class c(Duck,Mallard):
def c(self):
super().quack()
print(c().c()) #O/p = Duck Quack (as Duck appears 1st)
print("#=========super vs self vs class name============")
super()
super()
1. Used inside child
constructor to call parent constructor
2. Used inside overriding
method to call the parent method
self
1. Used to call method and
properties of the same class
2. Used to call methods and
properties of parent class
3. Cannot be used inside overriding
constructor or method as it creates infinite loop.
className
1. Can be used to call properties
and methods of parent class.
2. Parent class name needs to be
hardcoded
3. "self" needs to be
sent as an arguement.
print(#=========multiple inheritance============")
class a():
x="1"
def a(self):
return("2")
class b():
y="3"
def b(self):
return("4")
class c(a,b):
z="5"
def c(self):
print(self.x,self.y,self.z,self.a(),self.b())
C=c()
C.c()
print(C.x,C.y,C.a(),C.b())