-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheleven.py
More file actions
186 lines (148 loc) · 4.86 KB
/
eleven.py
File metadata and controls
186 lines (148 loc) · 4.86 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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# -----------------------------------------------INHERITANCE & MORE ON OOPS-------------------------------------------------------
'''
Inheritance is a way of creating a new class from an existing class.
'''
# Syntax:
class Employee: # Base class
# Code
def __init__(self):
pass
class Programmer (Employee): # Derived or child class
def __init__(self):
pass
'''
We can use the method and attributes of 'Employee' in 'Programmer' object.
Also, we can overwrite or add new attributes and methods in 'Programmer' class.
TYPES OF INHERITANCE
1) Single inheritance
2) Multiple inheritance
3) Multilevel inheritance
'''
# -----------------------------------------------Single inheritance-------------------------------------------------------
class Employee:
company = "ITC"
name="John"
salary=123
language="python"
def show(self):
print(f"The name of the Employee is {self.name} and the salary is {self.salary}")
# class Programmer:
# company = "ITC Infotech"
# def show(self):
# print(f"The name is {self.name} and the salary is {self.salary}")
# def showLanguage(self):
# print(f"The name is {self.name} and he is good with {self.language} language")
class Programmer(Employee):
company = "ITC Infotech"
def showLanguage(self):
print(f"The name is {self.name} and he is good with {self.language} language")
a = Employee()
b = Programmer()
print(a.company, b.company)
a.show()
b.showLanguage()
# -----------------------------------------------Multiple inheritance------------------------------------------------------
class Employee:
company = "ITC"
name = "Default name"
def show(self):
print(f"The name of the Employee is {self.name} and the company is {self.company}")
class Coder:
language = "Python"
def printLanguages(self):
print(f"Out of all the languages here is your language: {self.language}")
class Programmer(Employee, Coder):
company = "ITC Infotech"
def showLanguage(self):
print(f"The name is {self.company} and he is good with {self.language} language")
a = Employee()
b = Programmer()
b.show()
b.printLanguages()
b.showLanguage()
# -----------------------------------------------Multilevel inheritance-------------------------------------------------------
class Employee:
a = 1
class Programmer(Employee):
b = 2
class Manager(Programmer):
c = 3
o = Employee()
print(o.a) # Prints the a attribute
# print(o.b) # Shows an error as there is no b attribute in Employee class
o = Programmer()
print(o.a, o.b)
o = Manager()
print(o.a, o.b, o.c)
# -----------------------------------------------Super Method-------------------------------------------------------
# super() method is used to access the methods of a super class in the derived class.
class Employee:
def __init__(self):
print("Constructor of Employee")
a = 1
class Programmer(Employee):
def __init__(self):
print("Constructor of Programmer")
b = 2
class Manager(Programmer):
def __init__(self):
super().__init__()
print("Constructor of Manager")
c = 3
o = Manager()
print(o.a, o.b, o.c)
'''
output:
Constructor of Programmer
Constructor of Manager
1 2 3
'''
# -----------------------------------------------Class Method-------------------------------------------------------
# A class method is a method which is bound to the class and not the object of the class.
'''
Syntax:
@classmethod
def(cls,p1, p2):
'''
class Employee:
a = 1
@classmethod
def show(cls):
print(f"The class attribute of a is {cls.a}")
e = Employee()
e.a = 45
e.show() #The class attribute of a is 1
# -----------------------------------------------Property Decorators and @.GETTERS AND @.SETTERS--------------------------------------------------
class Employee:
@property
def name(self):
return f"{self.fname} {self.lname}"
@name.setter
def name (self,value):
self.fname = value.split(" ")[0]
self.lname = value.split(" ")[1]
e = Employee()
e.name = "John Fitzgerald"
print(e.fname, e.lname)
# -----------------------------------------------OPERATOR OVERLOADING IN PYTHON-------------------------------------------------------
'''
Operators in Python can be overloaded using dunder methods.
These methods are called when a given operator is used on the objects.
Operators in Python can be overloaded using the following methods:
p1+p2 # pl.__add__(p2)
p1-p2 # p1.__sub__(p2)
p1*p2 #p1.__mul__(p2)
p1/p2 #p1.__truediv_(p2)
p1//p2 # pl.__floordiv__(p2)
Other dunder/magic methods in Python:
str__() # used to set what gets displayed upon calling str(obj)
_len_() # used to set what gets displayed upon calling._len_() or len(obj)
'''
class Number:
def __init__(self, n):
self.n = n
def __add__(self, num):
return self.n + num.n
n = Number(1)
m = Number(2)
print(n + m)