Tuesday 13 October 2015

Static method,class method and instance method in python

There are 3 type of methods in python.we will discuss them step by step.I hope this artical will clear all confusions about the static method,class method and instance method.
now go forward to explaination about these 3 methods.

1. Class Method : It receives the class as an implicit first argument. It is bound to the class.

syntax
@classmethod
def functionname(cls,arg1,agr2,...):
    ................................
        function code
    ................................
  here cls equal to class.

Q. What does @classmethod do?
Ans. It is a decorator which changes a method to class method.


Q. How many ways we can call the class method. Explain?
Ans. we can call the class method using 2 ways.
          1. using the instance of class
          2. using the class itself


Practical program:

class B(object):
    @classmethod
    def fun(cls,a,b):
        print a,b
b = B()
print("using the instance of class")
b.fun(1,2)                                                           //using the instance of class
print("using the class ")
B.fun(1,2)                                                           //using the class 
output:
using the instance of class
1,2
using the class 
1,2
2. Static Method : It doesnot receives the class as an implicit first argument. It is unbound method.

syntax
@classmethod
def functionname(arg1,agr2,...):
    ................................
        function code
    ................................
  here no need of any implicit argument.

Q. What does @staticmethod do?
Ans. It is a decorator which changes a method to static method.


Q. How many ways we can call the static method. Explain?
Ans. we can call the static method using 2 ways same as class method.
          1. using the instance of class
          2. using the class itself


Practical program:

class B(object):
    @staticmethod
    def fun(a,b):
        print a,b
b = B()
print("using the instance of class")
b.fun(1,2)                                                           //using the instance of class
print("using the class ")
B.fun(1,2)                                                           //using the class 
output:
using the instance of class
1,2
using the class 
1,2
3. Instance Method : It  receives the instance as an implicit first argument. It is bound to the object.

syntax
def functionname(self,arg1,agr2,...):
    ................................
        function code
    ................................
  here self = instance of the class

Q. How many ways we can call the instance method. Explain?
Ans. we can call the instance method using 2 ways.
          1. using the instance of class
          2. using the class itself but we have to pass manually the class instance as a parameter in                       calling function
The instance method totally dependent on the object.in the program we can see that how we are passing the class instance in the calling function

Practical program:

class B(object):
    def fun(a,b):
        print a,b
b = B()
print("using the instance of class")
b.fun(1,2)                                                           //using the instance of class
print("using the class ")
B.fun(b,1,2)                                                       //using the class with additional class instance                                                                                    as a parameter
output:
using the instance of class
1,2
using the class 
1,2

No comments:

Post a Comment