Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
76 views

Oops in Python: First Show

The document discusses object-oriented programming concepts in Python like classes, objects, inheritance, polymorphism etc. with examples. It covers: - Defining classes and creating objects - Constructors and initializing objects - Instance and class variables/methods - Single, multi-level and multiple inheritance - Nested/inner classes - Constructor behavior in inheritance - Different types of methods like instance, class and static methods

Uploaded by

Kona Sai Eswar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

Oops in Python: First Show

The document discusses object-oriented programming concepts in Python like classes, objects, inheritance, polymorphism etc. with examples. It covers: - Defining classes and creating objects - Constructors and initializing objects - Instance and class variables/methods - Single, multi-level and multiple inheritance - Nested/inner classes - Constructor behavior in inheritance - Different types of methods like instance, class and static methods

Uploaded by

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

OOPS IN PYTHON

In [1]:

1 class first:
2 def show(self):
3 print("a is")
4 fir=first()
5 first.show(fir) #another way here we are passing "fir" which is going to self so sel
6 fir.show()

a is

a is

CONSTRUCTOR ( init(self): )
In [2]:

1 class initializing:
2 def __init__(self):
3 print("in init")
4
5 init=initializing()

in init

In [3]:

1 class computer:
2 def __init__(self,cpu,ram): #like a constructor
3
4 #"self" says it refers to that particular object OR CURRENT INSTANCE.As we know "self
5
6 self.cpu=cpu
7 self.ram=ram
8
9 def config(self):
10 print("cpu is ",self.cpu," ram is ",self.ram)
11
12 com1=computer("i5",16) #actually we are passing 3 parameters here 1)self=com1 2
13 com2=computer("rayzen",8) #self=com2
14
15 com1.config()
16 com2.config()

cpu is i5 ram is 16

cpu is rayzen ram is 8

In [4]:

1 #all objects will take space in heap memory


2
3 print(id(com1))
4
5 print(id(com2))

2859943603840

2859943528912

In [5]:

1 class human:
2 def __init__(self):
3 self.name="sai"
4 self.age=20
5 def update(self):
6 self.age=10
7
8 h1=human()
9 h2=human()
10
11 print(h1.name,h1.age,h2.name,h2.age)
12
13 h1.update()
14
15 print(h1.name,h1.age)

sai 20 sai 20

sai 10

In [6]:

1 class human:
2 def __init__(self):
3 self.name="sai"
4 self.age=20
5 def update(self):
6 self.age=10
7 # self.name="eswar"
8 def compare(self,sec_obj):
9 if self.age==sec_obj.age:
10 return True
11 else:
12 return False
13
14 h1=human()
15 h2=human()
16
17 print(h1.name,h1.age,h2.name,h2.age)
18
19 if h1.compare(h2): #in compare method h1=self and second_obj=h2
20 print("ages are same")
21 else:
22 print("not same")
23
24 h1.update #h1.age=10
25 print(h1.name,h1.age)
26
27 if h1.compare(h2): #in compare method h1=self and second_obj=h2
28 print("ages are same")
29 else:
30 print("not same")

sai 20 sai 20

ages are same

sai 20

ages are same

In [7]:

1 if h1==h2: #objects are being compared🤣


2 print("same")

types of variables

-instance

-class
In [8]:

1 class car:
2 wheels=4 #class variable(common for all objects one change reflects f
3
4 def __init__(self):
5 self.milage=10 #instance variables ,instance namespace
6 self.company="bmw"
7
8 c1=car()
9 c2=car()
10 print(c1.milage)
11 c1.milage=20
12 print(c1.milage)
13 print(c1.wheels,c2.wheels)
14 print(car.wheels,car.wheels)
15 car.wheels=5
16 print(car.wheels,c2.wheels)
17 c1.wheels=6
18 print(c1.wheels,c2.wheels,car.wheels)

10

20

4 4

4 4

5 5

6 5 5

types of methods

1)instance methods 2)class methods 3)static methods

in variables class and static variables are same but in method they are
different

two types of instance methods

-Acessors : only fetch

-mutators : can change the values

in below code "get" is accesor and "set" is mutator


In [9]:

1 class student:
2
3 school="RNSIT"
4
5 def __init__(self,m1,m2,m3):
6 self.m1=m1
7 self.m2=m2
8 self.m3=m3
9
10 def average(self): #instance as it work with objects
11 return (self.m1 + self.m2 + self.m3)/3
12
13 @classmethod #we use decorator for class method
14 def getschool(cls): #class method for class we us
15 return(cls.school)
16
17
18 def get(self):
19 return self.m1
20
21 def set(self,value):
22 self.m1=value #same as __init__
23
24 @staticmethod
25 def info(): #static method it doesnt deal with class or instance variable
26 print("this is student class .. in abc module")
27
28 s1=student(10,20,30)
29 s2=student(35,20,10)
30 print(s1.average(),s2.average())
31
32 print(s1.getschool())
33 print(student.getschool())
34
35 student.info()

20.0 21.666666666666668

RNSIT

RNSIT

this is student class .. in abc module

inner class
In [10]:

1 class student:
2
3 def __init__(self,name,rollno,brand,cpu,ram):
4 self.name=name
5 self.rollno=rollno
6 self.lap=self.laptop(brand,cpu,ram) #creating object for inner class inside
7
8 def show(self):
9 print("name: ",self.name,"","roll.no: ",self.rollno)
10 self.lap.show()
11
12 class laptop:
13
14 def __init__(self,brand,cpu,ram):
15 self.brand=brand
16 self.cpu=cpu
17 self.ram=ram
18
19 def show(self):
20 print("brand: ",self.brand,"cpu: ",self.cpu,"ram: ",self.ram)
21
22 s1=student("sai",1,"hp","i5",8)
23 s2=student("eswar",2,"dell","i5",4)
24 s3=student("kona",3,"mac","i12",8)
25
26 s1.show()
27 s2.show()
28 s3.show()
29
30 #s1.lap.brand
31 #object of inner class out side
32 lap1=s1.lap
33 lap2=s2.lap
34
35 lap1.show()
36
37 lap2.show()
38
39 lap3=student.laptop("lenovo","i12",8) #object of inner class outside outer clas
40
41 print(lap3.brand)
42
43 s1.lap.show()
44
45 lap3.show()

name: sai roll.no: 1

brand: hp cpu: i5 ram: 8

name: eswar roll.no: 2

brand: dell cpu: i5 ram: 4

name: kona roll.no: 3

brand: mac cpu: i12 ram: 8

brand: hp cpu: i5 ram: 8

brand: dell cpu: i5 ram: 4

lenovo

brand: hp cpu: i5 ram: 8

brand: lenovo cpu: i12 ram: 8

inheritance

single level inheritance


In [11]:

1 class A:
2 def feature1(self):
3 print("feature1")
4 def feature2(self):
5 print("feature2")
6 class B(A): #INHERITANCE CLASS A
7 def feature3(self):
8 print("feature3")
9 def feature4(self):
10 print("feature4")
11
12 print("object for class A")
13 a1=A()
14 a1.feature1()
15 a1.feature2()
16
17 print("\n")
18 print("OBJECT FOR CLASS B WHICH CAN ACCESS CLASS A METHODS ALSO")
19
20 b1=B()
21 b1.feature1()
22 b1.feature2()
23 b1.feature3()
24 b1.feature4()

object for class A

feature1

feature2

OBJECT FOR CLASS B WHICH CAN ACCESS CLASS A METHODS ALSO

feature1

feature2

feature3

feature4

multi level inheritance


In [12]:

1 class A:
2 def feature1(self):
3 print("feature1")
4 def feature2(self):
5 print("feature2")
6 class B(A): #INHERITANCE CLASS A
7 def feature3(self):
8 print("feature3")
9 def feature4(self):
10 print("feature4")
11 class C(B): #INHERITANCE CLASS B
12 def feature5(self):
13 print("feature5")
14 def feature6(self):
15 print("feature6")
16
17 c1=C()
18 c1.feature1()
19 c1.feature2()
20 c1.feature3()
21 c1.feature4()
22 c1.feature5()
23 c1.feature6()

feature1

feature2

feature3

feature4

feature5

feature6

multiple inheritance
In [13]:

1 class A:
2 def feature1(self):
3 print("feature1")
4 def feature2(self):
5 print("feature2")
6 class B:
7 def feature3(self):
8 print("feature3")
9 def feature4(self):
10 print("feature4")
11 class C(A,B): #INHERITANCE CLASS A,B
12 def feature5(self):
13 print("feature5")
14 def feature6(self):
15 print("feature6")
16
17 print("object for class A")
18 a1=A()
19 a1.feature1()
20 a1.feature2()
21
22 print("\n")
23 print("OBJECT FOR CLASS B")
24
25 b1=B()
26 # b1.feature1() these two we cannot access with B
27 # b1.feature2()
28 b1.feature3()
29 b1.feature4()
30
31 print("\n")
32 print("object of c which can access both A AND B")
33 c1=C()
34 c1.feature1()
35 c1.feature2()
36 c1.feature3()
37 c1.feature4()
38 c1.feature5()
39 c1.feature6()

object for class A

feature1

feature2

OBJECT FOR CLASS B

feature3

feature4

object of c which can access both A AND B

feature1

feature2

feature3

feature4

feature5

feature6

hierarchial inheritance
In [14]:

1 class A:
2 def feature1(self):
3 print("feature1")
4 def feature2(self):
5 print("feature2")
6 class B(A):
7 def feature3(self):
8 print("feature3")
9 def feature4(self):
10 print("feature4")
11 class C(A): #INHERITANCE CLASS A
12 def feature5(self):
13 print("feature5")
14 def feature6(self):
15 print("feature6")
16
17 a=A()
18
19 a.feature1()
20 a.feature2()
21
22 b=B()
23
24 b.feature1()
25 b.feature2()
26 b.feature3()
27 b.feature4()
28 # b.feature5() error
29 # b.feature6()
30
31 c=C()
32 c.feature1()
33 c.feature2()
34 # c.feature3() error
35 # c.feature4()
36 c.feature5()
37 c.feature6()

feature1

feature2

feature1

feature2

feature3

feature4

feature1

feature2

feature5

feature6

constructors in inheritance
In [15]:

1 class A:
2 def __init__(self):
3 print("constructor A")
4 def feature1(self):
5 print("feature1")
6 def feature2(self):
7 print("feature2")
8 class B(A): #INHERITANCE CLASS A
9 def __init__(self):
10 print("constructor B")
11 def feature3(self):
12 print("feature3")
13 def feature4(self):
14 print("feature4")
15
16 a1=A()
17 b1=B() #when u create obj of sub class it will call init of su

constructor A

constructor B

In [16]:

1 class A:
2 def __init__(self):
3 print("constructor A")
4 def feature1(self):
5 print("feature1")
6 def feature2(self):
7 print("feature2")
8 class B(A): #INHERITANCE CLASS A
9 def __init__(self):
10 print("constructor B")
11 def feature3(self):
12 print("feature3")
13 def feature4(self):
14 print("feature4")
15 class C(B): #INHERITANCE CLASS B
16 def __init__(self):
17 print("constructor c")
18 def feature5(self):
19 print("feature5")
20 def feature6(self):
21 print("feature6")
22
23 c=C()

constructor c

In [17]:

1 class A:
2 def feature1(self):
3 print("feature1")
4 def feature2(self):
5 print("feature2")
6 class B:
7 def feature3(self):
8 print("feature3")
9 def feature4(self):
10 print("feature4")
11 class C(A,B): #INHERITANCE CLASS A,B
12 def __init__(self):
13 print("constructor C")
14 def feature5(self):
15 print("feature5")
16 def feature6(self):
17 print("feature6")
18
19 c1=C()

constructor C

super
In [18]:

1 class A:
2 def __init__(self):
3 print("constructor A")
4 def feature1(self):
5 print("feature1")
6 def feature2(self):
7 print("feature2")
8 class B(A): #INHERITANCE CLASS A
9 def __init__(self):
10 super().__init__() #if u have "super" then it will call init of super
11 print("constructor B")
12
13 def feature3(self):
14 print("feature3")
15 def feature4(self):
16 print("feature4")
17
18 b1=B()

constructor A

constructor B

In [19]:

1 class A:
2 a=10
3 def __init__(self):
4 print("constructor A")
5 def feature1(self):
6 print("feature1")
7 def feature2(self):
8 print("feature2")
9 class B(A): #INHERITANCE CLASS A
10 def __init__(self):
11 super().__init__() #if u have "super" then it will call init of super
12 print("constructor B")
13
14 def feature3(self):
15 print("feature3")
16 def feature4(self):
17 print("feature4")
18
19 def method_to_show_super_can_also_used_to_call_methods (self):
20 super().feature1()
21 print(super().a)
22
23 b1=B()
24
25 b1.method_to_show_super_can_also_used_to_call_methods()

constructor A

constructor B

feature1

10

In [20]:

1 class A:
2 def __init__(self):
3 print("constructor A")
4 def feature1(self):
5 print("feature1")
6 def feature2(self):
7 print("feature2")
8 class B(A): #INHERITANCE CLASS A
9 def __init__(self):
10 super().__init__()
11 print("constructor B")
12 def feature3(self):
13 print("feature3")
14 def feature4(self):
15 print("feature4")
16 class C(B): #INHERITANCE CLASS B
17 def __init__(self):
18 super().__init__()
19 print("constructor c")
20 def feature5(self):
21 print("feature5")
22 def feature6(self):
23 print("feature6")
24 print("object for b")
25 b=B()
26 print("object for c")
27 c=C()

object for b

constructor A

constructor B

object for c

constructor A

constructor B

constructor c

In [22]:

1 class A:
2 def __init__(self):
3 print("constructor A")
4 def feature1(self):
5 print("feature1")
6 def feature2(self):
7 print("feature2")
8 class B:
9 def __init__(self):
10 print("constructor B")
11 def feature3(self):
12 print("feature3")
13 def feature4(self):
14 print("feature4")
15 class C(A,B): #INHERITANCE CLASS A,B
16 def __init__(self):
17 super().__init__()
18 print("constructor C")
19 def feature5(self):
20 print("feature5")
21 def feature6(self):
22 print("feature6")
23
24 c1=C()

constructor A

constructor C

In [23]:

1 class A:
2 def __init__(self):
3 print("constructor A")
4 def feature1(self):
5 print("feature1")
6 def feature2(self):
7 print("feature2")
8 class B:
9 def __init__(self):
10 print("constructor B")
11 def feature3(self):
12 print("feature3")
13 def feature4(self):
14 print("feature4")
15 class C(B,A): #INHERITANCE CLASS A,B
16 def __init__(self):
17 super().__init__()
18 print("constructor C")
19 def feature5(self):
20 print("feature5")
21 def feature6(self):
22 print("feature6")
23
24 c1=C()

constructor B

constructor C

we can see in case of multiple inheritance super.() will prefer left


side class like c(a,b) it gives prefernce to a and for c(b,a) in this
case b has preferance than a , it is called method resolution
order (MRO)

for methods also it gives peferance for left side one


In [24]:

1 class A:
2 def __init__(self):
3 print("constructor A")
4 def feature1(self):
5 print("feature1_A")
6 def feature2(self):
7 print("feature2")
8 class B:
9 def __init__(self):
10 print("constructor B")
11 def feature1(self):
12 print("feature1_B")
13 def feature4(self):
14 print("feature4")
15 class C(A,B): #INHERITANCE CLASS A,B
16 def __init__(self):
17 super().__init__()
18 print("constructor C")
19 def feature5(self):
20 print("feature5")
21 def feature6(self):
22 print("feature6")
23
24 c1=C()
25 c1.feature1() # it will call A's featuer1

constructor A

constructor C

feature1_A

POLYMORPHISM

duck typing
In [25]:

1 class pycharm:
2 def execute(self):
3 print("code executing")
4
5 class vscode:
6 def execute(self):
7 print("code executing")
8 print("can use extensions")
9
10 class laptop:
11 def code(self,ide):
12 ide.execute()
13
14 ide1=pycharm()
15 ide2=vscode()
16
17 lap1=laptop()
18 lap1.code(ide1)
19
20 print("\n")
21
22 lap2=laptop()
23 lap2.code(ide2)

code executing

code executing

can use extensions

operator over loading

print(5+"hello world") does not work


In [26]:

1 class student:
2 def __init__(self,m1,m2):
3 self.m1=m1
4 self.m2=m2
5
6 def __add__(self,second):
7 m1=self.m1+second.m1
8 m2=self.m2+second.m2
9 s3=student(m1,m2)
10 return s3
11 def __gt__(self,second):
12 m1=self.m1+second.m1
13 m2=self.m2+second.m2
14 if m1 > m1:
15 return True
16 else:
17 return False
18
19 def __str__(self):
20 return "{} {}".format(self.m1,self.m2)
21
22
23 s1=student(10,20)
24 s2=student(20,30)
25
26 s3=s1+s2
27
28 print(s3.m1,s3.m2)
29
30 print(s1>s2)
31
32 print(s1)

30 50

False

10 20

python doesnt have method overloading


In [27]:

1 class student :
2
3 def sum1(self,a,b):
4 return a+b
5 def sum1(self,a,b,c):
6 return a+b+c
7
8 s1=student()
9 # print(s1.sum1(2,3)) error's
10 # print(s1.sum1(2,3,4))

we can simply use "default parameters"


In [28]:

1 class student : #compile time polymorphism


2
3 def sum1(self,a=None,b=None,c=None):
4 if a!=None and b!=None and c!=None:
5 return a+b+c
6 elif a!=None and b!=None :
7 return a+b
8 else:
9 return a
10
11 s1=student()
12 print(s1.sum1(2))
13 print(s1.sum1(2,3))
14 print(s1.sum1(2,3,4))

method over-riding
In [29]:

1 class A: #run time polymorphism


2 def show(self):
3 print("in A")
4
5 class B(A):
6 def show(self):
7 print("in B")
8 super().show()
9
10 b=B()
11 b.show()

in B

in A

ABSTRACTION: hiding implementation and showing


only essential parts

ABC MODULE-ABSTRACT BASE CLASSES IS USED


FOR ABSTRACTION
In [30]:

1 from abc import ABC,abstractmethod


In [31]:

1 class computer(ABC):
2 @abstractmethod
3 def process(self): #ABSTRACT METHOD HENCE COMPUTER IS ABSTRACT CLASS
4 pass
5
6 class laptop(computer):
7 pass
8
9 # com=computer() Can't instantiate abstract class computer with abstract methods pr
10
11 #com1=laptop() as laptop inheriting computer it must define method "process" other

In [32]:

1 class computer(ABC):
2 @abstractmethod
3 def process(self):
4 pass
5
6 class laptop(computer): #CONCERETE CLASS(NO ABSTRACT METHODS)
7 def process(self): #OVER RIDING ABSTRACT METHOD
8 print("run")
9
10 lap=laptop()
11 lap.process()

run

In [34]:

1 class bank(ABC): #abstract class(cannot create obj)


2 @abstractmethod
3 def housinginterest(self):
4 pass
5 @abstractmethod
6 def vehicelinterest(self):
7 pass
8
9 class sbi(bank): #concerte class
10
11 def housinginterest(self):
12 print("housing interest in sbi is 0.5%")
13
14 # def vehicelinterest(self):
15 # print("vehical interest in sbi os 0.25%")
16
17 s=sbi()
18 s.housinginterest()
19 # s.vehicelinterest()
20
21 #this codes gives error bcoz we have to over-ride all the methods in abstract class i
22 #for derived also
23
24 # error:Can't instantiate abstract class sbi with abstract methods vehicelinterest
25

housing interest in sbi is 0.5%

In [35]:

1 class bank(ABC): #abstract class(cannot create obj)


2 @abstractmethod
3 def housinginterest(self):
4 pass
5 @abstractmethod
6 def vehicelinterest(self):
7 pass
8
9 class sbi(bank): #concerte class
10
11 def housinginterest(self):
12 print("housing interest in sbi os 0.5%")
13
14 def vehicelinterest(self):
15 print("vehical interest in sbi os 0.25%")
16
17 s=sbi()
18 s.housinginterest()
19 s.vehicelinterest()

housing interest in sbi os 0.5%

vehical interest in sbi os 0.25%

ENCAPSULATION
Encapsulation: wrapping of data and methods , data
hiding by access specifiers(public(with in class or
outside class) ,private(only with in class))
variable name --> public variable

method name --> private method

__variables name --> private variables

__method name -->private method

In [36]:

1 class demo:
2 a=10 # a and b are public variables
3 b=100
4 def display(self): # display is public method
5 print(self.a,self.b)
6
7 obj=demo()
8 print(obj.a)
9 print(obj.b)
10 obj.display()

10

100

10 100

In [38]:

1 class demo:
2 __a=10 # a is private variable , b is public
3 b=100
4 def display(self): # display is public method
5 print(self.a,self.b) # here a is accessed within class so no prob
6
7 obj=demo()
8 print(obj.a) # here a is outside so it will give error
9 print(obj.b)
10 obj.display()

--------------------------------------------------------------------------
-

AttributeError Traceback (most recent call las


t)

<ipython-input-38-3d191765f490> in <module>

7 obj=demo()

----> 8 print(obj.a) # here a is outside so it


will give error

9 print(obj.b)

10 obj.display()

AttributeError: 'demo' object has no attribute 'a'

In [39]:

1 class demo:
2 a=10 # a and b are public variables
3 b=100
4 def __display(self): # display is private method
5 print(self.a,self.b)
6 def show_public_method(self):
7 self.__display() #private method within class so no problem
8
9 obj=demo()
10 print(obj.a)
11 print(obj.b)
12 #obj.display() #private method outside class so error
13 obj.show_public_method() #this inturn access private method for us

10

100

10 100

You might also like