-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex_14
86 lines (66 loc) · 1.4 KB
/
ex_14
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Our first Python object
#!/usr/bin/python3
class PartyAnimal:
x = 0
def party(self):
self.x = self.x + 1
print('So far', self.x)
an = PartyAnimal()
an.party()
an.party()
an.party()
# Classes as types
PartyAnimal.party(an)
print('Type', type(an))
print('Dir', dir(an))
print('Type', type(an.x))
print('Type', type(an.party))
# Object lifecycle
#!/usr/bin/python3
class PartyAnimal:
x = 0
def __init__(self):
print('I am constructed')
def party(self):
self.x = self.x + 1
print('So far', self.x)
def __del__(self):
print('I am destructed', self.x)
an = PartyAnimal()
an.party()
an.party()
print('an type', type(an))
print('an = 42')
an = 42
print('an type', type(an))
# Multiple instances
#!/usr/bin/python3
class PartyAnimal:
x = 0
name = ''
def __init__(self, nam):
self.name = nam
print(self.name, 'constructed')
def party(self):
self.x = self.x + 1
print(self.name, 'party count', self.x)
s = PartyAnimal('Sally')
j = PartyAnimal('Jim')
s.party()
j.party()
s.party()
# Inheritance
#!/usr/bin/python3
from party import PartyAnimal
class CricketFan(PartyAnimal):
points = 0
def six(self):
self.points = self.points + 6
self.party()
print(self.name, 'points', self.points)
s = PartyAnimal('Sally')
s.party()
j = CricketFan('Jim')
j.party()
j.six()
print(dir(j))