-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15.oops_intro_python.py
More file actions
62 lines (38 loc) · 1.16 KB
/
15.oops_intro_python.py
File metadata and controls
62 lines (38 loc) · 1.16 KB
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
# Example 1: Creating Class and Object in Python
print('>>>> Example - 1 >>>>')
class Parrot:
species = "bird"
def __init__(self, name, age):
self.name = name
self.age = age
blu = Parrot("Blu", 10)
woo = Parrot("Woo", 15)
# access the class attributes
print("Blu is a {}".format(blu.species))
print("Woo is also a {}".format(woo.species))
# access the instance attributes
print("{} is {} years old".format(blu.name, blu.age))
print("{} is {} years old".format(woo.name, woo.age))
# Example 2: Creating Methods in Python
print('\n')
print('>>>> Example - 2 >>>>')
class Parrot:
def __init__(self, name, age):
self.name = name
self.age = age
def sing(self, song):
return "{} sings {}".format(self.name, song)
def dance(self):
return "{} is now dancing".format(self.name)
blu = Parrot("Blu", 10)
print(blu.sing("'Happy'"))
print(blu.dance())
# Example 3: Constructors Destructors in Python
print('\n')
print('>>>> Example - 3 >>>>')
class Employee:
def __init__(self):
print('Employee created.')
def __del__(self):
print('Destructor called, Employee deleted.')
obj = Employee()