Static mehtod:Static methods are used when we need to process data associated with classes instead of instances. A static method has no self argument and it is nested in a class and is designed to work on class attributes instead of instance attributes.
Static methods never receive an automatic self argument, whether called through a class or an instance. They usually keep track of information that spans all instances, rather than providing behavior for instances.
The syntax of @staticmethod is:
@staticmethod
def function(args, ...)
static method can be called in two ways:1)using classname.methodname()
2)objectname.methodname()
ClassMethod:classmethod are decorated with @classmethod before method name.The @classmethod decorator, is a builtin function decorator that is an expression that gets evaluated after your function is defined. The result of that evaluation shadows your function definition.
class methods take a
cls
parameter that points to the class—and not the object instance—when the method is called.- A class method is a method which is bound to the class and not the object of the class.
- They have the access to the state of the class as it takes a class parameter that points to the class and not the object instance.
@classmethod
def methodname()
instance methods:instacne methods are by default takign self object as parameter.you can call these methods by using instance of classname.
No comments:
Post a Comment