/
classandfuncs.py
136 lines (101 loc) · 3.11 KB
/
classandfuncs.py
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
print("A Simple Function")
def add(a,b):
print("Parameter 1:",a)
print("Parameter 2:",b)
return a + b
print(add(10,20))
print(add("abc",str(10)))
print("Classes & Functions")
class pyclass:
name='inceptez class';
def Addnums(self,x, y): #The self parameter is a reference to the current instance of the class, and is used to access variables that belongs to the class.
return x + y
obj1=pyclass()
print(obj1.name)
print("calling Addnums function " + str(obj1.Addnums(10, 20)))
print("Duck typing")
#Duck type Language, When you use duck typing, you do not check (class) types at all. Instead, you check for the presence of a supporting methods, in the below + supports both types
def calc(a,b,c):return a+b+c
print(calc(10,20,30))
calcout=calc("inceptez ","tech","pvt ltd")
print(calcout)
print("OOPS in Python:")
print("Polymorphism")
print(len("Python"))
print(len(["Python", "Java", "Scala"]))
print(len({"Name": "Inceptez", "City": "Chennai"}))
lst = [5,4,2,3,1]
def func1() :
if lst.count(6) == 1:
print("method if block")
elif lst.count(5)==1:
print("method elif block")
else:
print("method else block")
func1()
def func1(arg1:str):
print (arg1)
returnval=func1('hello method')
print(returnval)
print("Inheritance")
print("Single Inheritance")
# Base class
class Parent:
def func1(self):
print("This function is in parent class.")
# Derived class
class Child(Parent):
def func2(self):
print("This function is in child class.")
# Driver's code
object = Child()
object.func1()
object.func2()
# multiple inheritance
print("Multiple Inheritance")
# Base class1
class Mother:
mothername = ""
def mother(self):
print(self.mothername)
# Base class2
class Father:
fathername = ""
def father(self):
print(self.fathername)
# Derived class
class Son(Mother, Father):
def parents(self):
print("Father :", self.fathername)
print("Mother :", self.mothername)
# Driver's code
s1 = Son()
s1.fathername = "x"
s1.mothername = "y"
s1.parents()
# multilevel inheritance
print("Multilevel Inheritance")
# Base class
class Grandfather:
def __init__(self, grandfathername):
self.grandfathername = grandfathername
# Intermediate class
class Father(Grandfather):
def __init__(self, fathername, grandfathername):
self.fathername = fathername
# invoking constructor of Grandfather class
Grandfather.__init__(self, grandfathername)
# Derived class
class Son(Father):
def __init__(self, sonname, fathername, grandfathername):
self.sonname = sonname
# invoking constructor of Father class
Father.__init__(self, fathername, grandfathername)
def print_name(self):
print('Grandfather name :', self.grandfathername)
print("Father name :", self.fathername)
print("Son name :", self.sonname)
# Driver code
s1 = Son('Z is the son of Y', 'Y is the son of X', 'X')
print(s1.grandfathername)
s1.print_name()