Wednesday, April 4, 2012

Class and Methods

Hello Everyone, in this post I will discussing about the Classes used in OOP In some more details.

From the last post, we already know how a class looks like and what all we can have in a Class, like Methods, Arguments.

So lets create a Class and then I will explain what happens, and Remember that a Class is a Blueprint, that should have the capability to be used with multiple objects, so our class has to be flexible.

>>> class area:
            def sq(self, l,b):
                  self.l = l
                  self.b = b
                  area = self.l*self.b
                  return area

So the first line is the class header, and the name of the class is area. Second line we have defined a method name sq which takes 2 arguments l and b.

Hold on , Hold on, I see here three arguments self, l and b, so how do you say it takes 2 arguments, You must be thinking this isn't it??

Ok the mystery reveals, self is a kind of temporary variable that hold the object name that will be used with the class, remember I said that the code has to be flexible. so (self, l, b) here means (objectname, l and b) so we have made the method dynamic as it can use any object and relate to it.

Next line we have self.l = l, which means objectname.l = l of the objectname(We are letting Python know that l means the l of the objectname) and same is for the next line too.

Next we have the formula, which calculates the area by self.l*self.b(which means l of the object*b of the object) and assigns it to a variable area.

Finally we have the return statement which returns the value of the area.

Now how do we use the class? To use the class, we have to create objects, so lets make 2 objects, one for square and one for rectangle.

>>> square = area()
>>> # we have create an object named square
>>> square.sq(10,10)
>>> # we call the method of the object with the DOT(.) operator, object.method, in this case square.sq(10, 10) by passed the value of l and b
>>> # self.l = 10, and self.b = 10 and after calculating the area it returned the value
>>> # as i said self.l means objectname.l, so if we now try square.l it should return the value 10
>>> square.l
>>> # You see, it returned 10, now if we use another object, what would happen, will these values of square object change? No, it wont, because, our self argument helps to keep them separate.
>>> rect = area()
>>> rect.sq(15, 25)
>>> square.l
>>> rect.l
>>> square.b
>>> rect.b

And We can create as many objects we want using one class.

NOTE: Again self, is important in class, otherwise its useless, the whole purpose of class gets void, so whenever we have a method in a class, the argument self is a must, even if it does not take any argument.

An example:

>>> class example:
              def p(self):
                          print "You see its must"

>>> test = example()
>>> test.p()
You see its must

So I hope This Makes the concept clear, Practice It Well, Try To Understand them properly, as slowly we are diving down into Python,

Thank You!


Post a Comment