Skip to content

Commit 78da6d2

Browse files
authored
Merge pull request #1 from VinitraMk/master
python oopm
2 parents 449add4 + 923c796 commit 78da6d2

File tree

1 file changed

+279
-0
lines changed

1 file changed

+279
-0
lines changed

oopm.md

Lines changed: 279 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,279 @@
1+
## Object Oriented Programming Methodology
2+
3+
### Class
4+
5+
A class is a user-defined data type. Its a collection of
6+
objects, where objects can represent just about anything
7+
from integers to real world objects like a ball. Structure of Classes
8+
in python are very similar to that of c++ or java.
9+
10+
For example:
11+
12+
```python
13+
class world:
14+
def hello(self):
15+
print("Hello World!")
16+
17+
obj=world()
18+
obj.hello()
19+
```
20+
21+
Output:
22+
```
23+
Hello World!
24+
```
25+
As we can see above, we create a class using the keyword class and the name of the class.
26+
This is followed by an indented block of statements which forms the body of the class.
27+
The body can contain methods and variables pertaining to the class. In the above example
28+
we have created a class "world" which defines a method "hello" to print the
29+
statement "Hello World!".
30+
We create an object of this class, which can access the data members and methods of the class
31+
and call the methods of the class by using the "." operator just like in c++.
32+
33+
#### Note:
34+
```
35+
1. The class methods in python need to have an extra parameter called self. This does not
36+
have a value, when a method is called.
37+
2. If we have a method with no parameters, in the class we must have one argument that is
38+
the self argument. See hello() method in the above example.
39+
3. This is similar to this pointer in c++ or this reference in Java.
40+
4. When we call a method MyClass.method(arg1,arg2) this is automatically converted
41+
by python into MyClass.method(object,arg1,arg2) where object is the self parameter.
42+
```
43+
44+
### The __init__ method
45+
46+
The __init__ method in python is similar to constructors in java or c++.
47+
That means it is run as soon as an object of the class is instantiated.
48+
This method is useful to initialize your objects in the class, before
49+
proceed further method calls.
50+
51+
For example:
52+
```python
53+
class name:
54+
def __init__(self,str):
55+
self.str=str
56+
57+
def greet(self):
58+
print('Hi, my name is ',self.str)
59+
60+
p=name('Vinitra')
61+
p.greet()
62+
```
63+
Output:
64+
```
65+
Hi, my name is Vinitra
66+
```
67+
### Inheritance
68+
69+
1. Introduction
70+
71+
Inheritance is one of the most powerful concepts of object oriented programming.
72+
It is the mechanism of deriving features of one class from another class facilitating
73+
code reusability.
74+
It constitutes defining a new class which is similar or identical to a class that
75+
already exists. The new class is hence called derived class or child class and the one
76+
from which it is inherited is called the base class or the parent class.
77+
78+
For example:
79+
```python
80+
class side:
81+
def __init__(self,l,b):
82+
self.l=l
83+
self.b=b
84+
85+
class area(side):
86+
87+
def __init__(self,l,b):
88+
side.__init__(self,l,b)
89+
90+
def Area(self):
91+
print('Area of the rectange is ',self.l*self.b)
92+
93+
a=area(4,5)
94+
a.Area()
95+
```
96+
Output:
97+
```
98+
Area of the rectangle is 20
99+
```
100+
2. Super()
101+
102+
```super()``` method is used to access data members and functions of parent class.
103+
The example given in the example for inheritance can also be executed by the
104+
```super()``` method.
105+
106+
For example:
107+
```python
108+
class side:
109+
def __init__(self,l,b):
110+
self.l=l
111+
self.b=b
112+
113+
class area(side):
114+
115+
def __init__(self,l,b):
116+
super(area,self).__init__(l,b)
117+
118+
def Area(self):
119+
print('Area of the rectange is ',self.l*self.b)
120+
121+
a=area(4,5)
122+
a.Area()
123+
```
124+
Output:
125+
```
126+
Area of the rectangle is 20
127+
```
128+
### Operator Overloading
129+
130+
Python operators work for built-in classes. But the same operators can be made to behave
131+
in a user-defined way. For example, the + operator adds the primitive variables of
132+
primitive data types. But by operator overloading you can make the + operator to
133+
add two complex numbers!
134+
This feature that allows operators to have to different functions according to context
135+
is called Operator Overloading.
136+
See the example below to see how to overload operators.
137+
Example:
138+
```python
139+
class Point:
140+
def __init__(self,x,y):
141+
self.x=x
142+
self.y=y
143+
144+
def __str__(self):
145+
return "({0},{1})",format(self.x,self.y)
146+
147+
def __add__(self,other):
148+
x=self.x+other.x
149+
y=self.y+other.y
150+
print()
151+
152+
p1=Point(2,4)
153+
p2=Point(-1,5)
154+
print(p1+p2)
155+
```
156+
Output:
157+
```
158+
(1,9)
159+
```
160+
What happens is that, when you write p1+p2, python will call p1.__add__(p2) which in
161+
turn is Point.__add__(p1,p2). Also in the above example, __str__ is special function
162+
like the __init__ method i.e it gets called whenever the class is instantiated.
163+
This method is used to control the output format
164+
165+
### Special Functions
166+
167+
Special functions are those methods which get executed as soon as the class is
168+
instantiated. For example: the __init__ method.
169+
There are many useful special functions in python. Given below are some of them.
170+
171+
1. __iter__: This method returns the iterator object and is implicitly called
172+
at the start of loops.
173+
2. __next__: This method returns the next value and is implicity called at
174+
each loop increment.
175+
176+
Example:
177+
```python
178+
class Counter:
179+
def __init__(self, low, high):
180+
self.current = low
181+
self.high = high
182+
183+
def __iter__(self):
184+
return self
185+
186+
def __next__(self): # Python 2: def next(self)
187+
if self.current > self.high:
188+
raise StopIteration
189+
else:
190+
self.current += 1
191+
return self.current - 1
192+
193+
for c in Counter(3, 8):
194+
print(c)
195+
```
196+
Output:
197+
```
198+
3
199+
4
200+
5
201+
6
202+
7
203+
8
204+
```
205+
206+
3. __str__ & __repr__: Both the functions are similar, i.e both are used to "represent"
207+
an object in a string. __repr__ gives you an official representation while __str gives
208+
informal representation. str() is used for creating output for end user while repr()
209+
is mainly used for debugging and development. Main goal of repr() is to be
210+
unambiguous and that of str() is to be readable. For example if we suspect a float
211+
has a small rounding error, repr will show us while str may not.
212+
For example:
213+
```python
214+
x=1
215+
print(repr(x))
216+
print(str(x))
217+
y='string'
218+
print(repr(y))
219+
print(str(y))
220+
```
221+
Output:
222+
```
223+
1
224+
1
225+
'string'
226+
string
227+
```
228+
In the above example, the return of repr() and str() are identical for int x, but not for
229+
string y. Therefore the default implementation of __repr__ for a str object can be called as
230+
an argument to eval and the return value would be a valid str object.
231+
Look at another example here:
232+
```python
233+
class Complex:
234+
235+
def __init__(self, real, imag):
236+
self.real = real
237+
self.imag = imag
238+
239+
# For call to repr(). Prints object information
240+
def __repr__(self):
241+
return '%s + i%s' % (self.real, self.imag)
242+
243+
# For call to str(). Prints readable form
244+
def __str__(self):
245+
return '%s + i%s' % (self.real, self.imag)
246+
247+
248+
t = Complex(10, 20)
249+
250+
print str(t)
251+
print(t)
252+
print repr(t)
253+
```
254+
Output:
255+
```
256+
10 + i20
257+
10 + i20
258+
Rational(10,20)
259+
```
260+
You can redefine __str__ and __repr__ to suit you needs as done in the above
261+
example.
262+
263+
4. dir(): This method takes only 1 object as a parameter and returns a list of
264+
attributes of the object.
265+
For example:
266+
```python
267+
class num:
268+
def __init__(self):
269+
return [1,2,3]
270+
271+
n=num()
272+
print(dir(n))
273+
```
274+
Output:
275+
```
276+
[1,2,3]
277+
```
278+
279+

0 commit comments

Comments
 (0)