Skip to content

Commit 48f5e57

Browse files
committed
Week 1- Object Oriented Python
1 parent 5edc1e9 commit 48f5e57

File tree

4 files changed

+131
-0
lines changed

4 files changed

+131
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Wed Jan 10 19:23:19 2018
4+
5+
@author: Jimit
6+
"""
7+
8+
class PartyAnimal:
9+
x = 0
10+
11+
def party(self):
12+
self.x = self.x + 1
13+
print('So far', self.x)
14+
15+
an = PartyAnimal()
16+
17+
an.party() #Equivalent to PartyAnimal.party(an)
18+
an.party()
19+
an.party()
20+
PartyAnimal.party(an)
21+
22+
print('Type:', type(an))
23+
print('Dir:', dir(an))
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Wed Jan 10 19:34:16 2018
4+
5+
@author: Jimit
6+
"""
7+
8+
class PartyAnimal:
9+
x = 0
10+
11+
# Constructor
12+
def __init__(self):
13+
print('I am constructed')
14+
15+
# Method
16+
def party(self):
17+
self.x = self.x + 1
18+
print('So far', self.x)
19+
20+
# Destructor
21+
def __del__(self):
22+
print('I am destructed', self.x)
23+
24+
an = PartyAnimal()
25+
an.party()
26+
an.party()
27+
28+
an = 42
29+
30+
print('an contains', an)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Wed Jan 10 19:42:27 2018
4+
5+
@author: Jimit
6+
"""
7+
8+
class PartyAnimal:
9+
x = 0
10+
name = ''
11+
12+
def __init__(self, z):
13+
self.name = z
14+
print(self.name, 'constructed')
15+
16+
def party(self):
17+
self.x = self.x + 1
18+
print(self.name, 'party count:', self.x)
19+
20+
s = PartyAnimal('Sally')
21+
s.party()
22+
23+
j = PartyAnimal('Jim')
24+
j.party()
25+
s.party()
26+
27+
PartyAnimal.party(j)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Wed Jan 10 19:50:52 2018
4+
5+
@author: Jimit
6+
"""
7+
8+
# Inheritance
9+
10+
class PartyAnimal:
11+
x = 0
12+
name = ''
13+
14+
def __init__(self, z):
15+
self.name = z
16+
print(self.name, 'constructed')
17+
18+
def party(self):
19+
self.x = self.x + 1
20+
print(self.name, 'party count:', self.x)
21+
22+
# FootballFan is a class that extends to PartyAnimal
23+
# It has all capabilities of PartyAnimal and more..
24+
class FootballFan(PartyAnimal):
25+
points = 0
26+
27+
def touchdown(self):
28+
self.points = self.points + 7
29+
self.party()
30+
print(self.name, 'points', self.points)
31+
32+
s = PartyAnimal('Sally')
33+
s.party()
34+
35+
j = FootballFan('Jim')
36+
j.party()
37+
j.touchdown()
38+
39+
40+
41+
"""
42+
43+
OUTPUT
44+
45+
Sally constructed
46+
Sally party count: 1
47+
Jim constructed # Since there is no constructor of FootballFan, so constructor of PartyAnimal is called
48+
Jim party count: 1
49+
Jim party count: 2
50+
Jim points 7
51+
"""

0 commit comments

Comments
 (0)