How to call methods without creating objects ?
First what is the difference between method and function ?
method --> assosiated with either object or class
function --> called independently
print "This is static method"
print "This is class method, class name = ",cls
def normal_method(args = ""):
print "this is normal method"
Test.static_method() # call without Test's object
#Test.normal_method() #unbound method normal_method()
#must be called with Test instance as first argument
test_obj = Test() # create instance of Test class
The main difference between normal method vs static and class method are we can call static and class methods
without creating objects of class.
When it will be use full ?
Can be usefull to have sett of simillar functions with in a class,
Utility functions bundled in class may be. or when methods are not depended on instance of class.
Now we will look in to the difference between static and class method
Nothing much except class method recevies argument, which is calling class name.
But static not.
How to write static and class methods ?
We make of decorators @classmethod and @staticmethod.
This is static method
This is class method, class name = __main__.Test
this is normal method