Python Tutorial

Tuesday, August 28, 2012

Python static method and class method



Both meythod have some similarities, we can use them without instantiate the object.
Static method know nothing about class and class method knows.
Lets go to a example code:


 
#staticVsClassMethod.py
class MyClass(): def anyFunction(self): print "I am any function" @staticmethod def myStaticFunction(): print "I am static method" @classmethod def myClassFunction(cls): print "I am class method" print cls MyClass.myStaticFunction() MyClass.myClassFunction() #MyClass.anyFunction() #this is not possible, need a object instance


Output:
I am static method
I am class method
__main__.MyClass

0 comments:

Post a Comment