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
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:
syntax
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:
syntax
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:
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
here cls equal to class.@classmethoddef functionname(cls,arg1,agr2,...):................................function code................................
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:
2. Static Method : It doesnot receives the class as an implicit first argument. It is unbound method.class B(object):@classmethoddef fun(cls,a,b):print a,bb = B()print("using the instance of class")b.fun(1,2) //using the instance of classprint("using the class ")B.fun(1,2) //using the classoutput:using the instance of class1,2using the class1,2
syntax
here no need of any implicit argument.@classmethoddef functionname(arg1,agr2,...):................................function code................................
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:
3. Instance Method : It receives the instance as an implicit first argument. It is bound to the object.class B(object):@staticmethoddef fun(a,b):print a,bb = B()print("using the instance of class")b.fun(1,2) //using the instance of classprint("using the class ")B.fun(1,2) //using the classoutput:using the instance of class1,2using the class1,2
syntax
here self = instance of the classdef functionname(self,arg1,agr2,...):................................function code................................
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,bb = B()print("using the instance of class")b.fun(1,2) //using the instance of classprint("using the class ")B.fun(b,1,2) //using the class with additional class instance as a parameteroutput:using the instance of class1,2using the class1,2
No comments:
Post a Comment