-
Notifications
You must be signed in to change notification settings - Fork 0
/
OOP_CONCEPT_CODE.py
187 lines (116 loc) · 3.96 KB
/
OOP_CONCEPT_CODE.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
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
# from datetime import date
# class Student:
# no_of_students = 0
# def __init__(self, name, age, courses):
# self.__name = name
# self.__age = age
# self.__courses = courses
# Student.no_of_students += 1
# def describe(self):
# print(f"My name is {self.__name} and age is {self.__age}")
# @classmethod
# def create_veg(cls, name, birthyear):
# age = date.today().year - birthyear
# return cls(name, age, ['Vegetable Course'])
# class Pizza:
# def __init__(self, ingredients):
# self.ingredients = ingredients
# @classmethod
# def veg(cls):
# return cls(['tomato', 'cheese'])
# def __str__ (self):
# return f"pizza ingredients are {self.ingredients}"
# pizza_1 = Pizza(['mato', 'kf'])
# pizza_2 = Pizza.veg()
# print(pizza_1,pizza_2)
# # staticmethod
# class Dates:
# def __init__(self, date):
# self.__date = date
# def getDate(self):
# return self.__date
# @staticmethod
# def toDashDate(date):
# return date.replace("/", "-")
# date = Dates("15-12-2016")
# datefromDB = "15/12/2016"
# dateWithDb = Dates.toDashDate(datefromDB)
# if date.getDate() == dateWithDb:
# print("equal")
# else:
# print("not equal")
# inhertaince &polymerphithm
# class Person:
# def __init__(self, name, age):
# self.name = name
# self.age = age
# def display(self):
# return f"Name is {self.name} and age is {self.age}"
# class Man(Person):
# gender = 'male'
# def __init__(self, name, age, voice):
# super().__init__(name, age)
# self.voice = voice
# def display(self):
# string = super().display()
# return string + f", voice is {self.voice} and gender is {self.gender}"
# man = Man("Ahmed", 30, "hard")
# print(man.display())
# class Women(Person) :
# gender = 'female'
# def __init__(self, name, age, hair):
# super().__init__(name, age)
# self.hair = hair
# def display(self):
# string = super().display()
# return string + f" hair is {self.hair} and gender is {self.gender}"
# women = Women("hean", 40, "red")
# print(women.display())
# print(isinstance(women,Women))
# abstraction
# from abc import ABC, abstractmethod
# class Vehicle(ABC):
# def __init__(self, name):
# self.name = name
# @abstractmethod
# def start(self):
# pass
# @abstractmethod
# def stop(self):
# pass
# class Car(Vehicle):
# def start(self):
# print(f"{self.name} is starting the engine.")
# def stop(self):
# print(f"{self.name} is stopping the engine.")
# class Motorcycle(Vehicle):
# def start(self):
# print(f"{self.name} is starting the ignition.")
# def stop(self):
# print(f"{self.name} is turning off the ignition.")
# car = Car("Car 1")
# motorcycle = Motorcycle("Motorcycle 1")
# car.start()
# car.stop()
# motorcycle.start()
# motorcycle.stop()
# __add__ over_ride
# less_than -> lt
class Man:
def __init__(self, name, age):
self.name = name
self.age = age
def __add__(self, other):
names = self.name + " and " + other.name
ages = str(self.age) + " and " + str(other.age)
return f"Names: {names}, Ages: {ages}"
def __lt__(self,other): # less_than -> lt for compare
return self.age <other.age
def display(self):
return f"Name: {self.name}, Age: {self.age}"
man = Man("Ahmed",500)
man1 = Man("John",40)
# print(man + man1)
# print(man <man1)
# you can see alot dunder function via write print(dir(class_name))
print (dir(Man))