Tuesday, April 3, 2012

OOP in Python


Hello Friends, in this post I will be discussing about Object Oriented Programming in Python.

Its very interesting, as well as very important, so pay attention and try to understand the things clearly.

Lets try to understand with the way Microsoft Word works, we have some per-defined templates in MS-Word right?? And we import one of the template, according to our choice and need, and work on it, but while doing this, are we making any changes to the template?? We create an instance of the template where we work and save the document, and this document has the characteristics of the template that we used, but the original template remains unaffected during the whole process.

Similarly, the same concept goes for Object Oriented Programming in Python, where we create an instance of a Class, and work on it.Say like, we have a program, where we have to find out the area of a square and a rectangle. well we can obviously create two separate functions for finding out the area for each. The work would be much easier, if we have the format (Length*Breadth) and we just import it and find out the area for each, isn't it?

From the things we learnt so far, what we can do is this..

>>> def area(lent, br):
           f = lent*br
           return f

>>> print "Area of rectangle is: ", area(length, breadth)
Area of rectangle is:  200
>>> l = 10
>>> b = 10
>>> print "Area of square is: ", area(l, b)
Area of square is:  100
>>> def area(lent=0, br=0):
           f = lent*br
           return f

>>> print "Area of square is: ", area()
Area of square is:  0
>>>

Notice here, how it worked, the function has been defined once, and it worked well, for both square and rectangle. So it was like a blue print or format that has been used and we used functions to make it, and this way of programming is known as Procedural Programming.

Now lets get into OOP,

In Object Oriented Programming(OOP), first of all we have to make something called Class, and this Class may contain data, or methods(think of methods as functions, the only difference is, they are called methods when they are used inside a class). And now after making the class, we can make objects out of it, seems like confusing isn't it? Don't worry, stick to the basics, gradually with future posts all the confusions will be sorted out.

Lets check this example, first we are going to make a class.

>>> class Girl:
           name = " "
           eyes = " "
           hairs = " "
           a = " "
           def SheSays(self):
                print "I Love you Everytime"

>>>

Okay, funny haa?? anyways I think this should make the basics clear...

Note: the method defined inside the class has an argument named self, it means no arguments has to be passed to get the statements inside the method. And all methods in a class should have an argument named self, its a must. We will come to know why and in more details in our future posts.

What we did is, we made a class named Girl, the attributes of class Girl are name, eyes, hairs, a, and we also have a method inside the class, which would print "She says I Love you Everytime" when the method is called.

Ok, so now that we have defined a class, we need to create an Object which would inherit the properties of the class Girl. So how do we define the object? Simple. We define a variable, and assign it to the class name

>>> MyFirstGirl = Girl()

After we have created the Object MyFirstGirl, we can create an instance of the class, and how do we do that?? By using the DOT(.) separator for each attributes or methods inside the class.

>>> MyFirstGirl.name = "Ashley"
>>> MyFirstGirl.eyes = "Brown"
>>> MyFirstGirl.hairs = "Medium"

Now we have set the attributes to the Object MyFirstGirl, and when we call them with their attributes name, it returns the value.

>>> MyFirstGirl.name
'Ashley'
>>> MyFirstGirl.eyes
'Brown'
>>> MyFirstGirl.hairs
'Medium'
>>> MyFirstGirl.SheSays()
I Love you Everytime

We can reuse the same class Girl again to define another object, lets say MySecondGirl, and set values to the attributes.

>>> MySecondGirl = Girl()
>>> MySecondGirl.name = "Diana"
>>> MySecondGirl.eyes = "Blue"
>>> MySecondGirl.hairs = "long"
>>> MySecondGirl.a = "Attractive"
>>> MySecondGirl.name
'Diana'
>>> MySecondGirl.eyes
'Blue'
>>> MySecondGirl.hairs
'long'
>>> MySecondGirl.a
'Attractive'
>>> MySecondGirl.SheSays()
I Love you Everytime
>>>

Notice that, the First object created with the same class is still the same and unchanged, and also the main class is uneffected. Let try to call the first object and see.

>>> MyFirstGirl.name
'Ashley'
>>> MySecondGirl.name
'Diana'
>>>

So isn't it Interesting?? A Class Created Once, can be used with several Objects.

Here is a short example to cover up the post in a nutshell.

>>> class area:
           l = " "
           b = " "
           r = " "

   
>>> Square = area()
>>> Square.l = 20
>>> Square.b = 20
>>> print "Area of the Square is: ", Square.l*Square.b
Area of the Square is:  400
>>>
>>> Rectangle = area()
>>> Rectangle.l = 30
>>> Rectangle.b = 50
>>> print "Area of the Rectangle is: ", Rectangle.l*Rectangle.b
Area of the Rectangle is:  1500
>>>
>>> pi = 3.14
>>> Circle = area()
>>> Circle.r = 15
>>> print "Area of the Circle is: ", pi*(Circle.r*Circle.r)
Area of the Circle is:  706.5
>>>

Okay So, I hope This was fun, and you have understood the basics of how things works in OOP.

Practice it, make your own programs.

Thank You, and Don't Forget to check My next Post.

0 comments:

Post a Comment