The most common method in a class is an instance method, that is, a method that passes an instance as the first parameter.
As an example, a basic example approach would be to the following:
ClassKls(Object):Def__init__(Self,data): self. Data = data def printd ( self): print (self data) ik1 = kls ( Span class= "S1" > ' Arun ' ) ik2 = kls ( Seema ' ) ik1. Printd () ik2. Printd ()
This gives the following output:
Arun
Seema
Then take a look at the code and sample pictures:
- The parameters are passed to the method .
- 3 The self parameter points to the current instance itself .
- 4 We do not need to pass the instance itself to the method, and the Python interpreter will do it by itself .
What if we now want to write a method that interacts only with the class rather than with the instance ?
You can use the @classmethod adorner to create a class method .
ClassKls(Object):No_inst=0Def__init__(Self):Kls.no_inst = kls. No_inst + 1 @classmethod def get _no_of_instance (cls_obj): return cls_obj. No_instik1 = kls () ik2 = kls () print ik1 . get_no_of_instance () print kls.< span class= "n" >get_no_of_instance ()
Output:
2
2
The advantage of this is that whether this is called from an instance or from a class, it uses the first parameter to pass the class over .
@staticmethod
There are often some features that are related to the class, but static methods are required when the runtime does not require instances and classes to participate. For example, changing environment variables or modifying properties of other classes can be used in static methods . This can be solved directly with a function, But it can also spread the code inside the class, causing maintenance difficulties .
such as this:
IND=' On 'DefCheckind():Return(IND==' On ')ClassKls(Object):Def__init__(Self,Data):Self.Data=DataDefDo_reset(Self):IfCheckind():Print(' Reset done for: ',Self.Data)def set_db(self): if checkind(): self. DB = ' new db connection ' print(' DB connection made for: ',self. Data)ik1 = Kls(ik1). Do_reset()ik1. set_db()
Output:
Reset done For:12
DB Connection made For:12
If you use @staticmethod, you can put the relevant code in the corresponding position.
.
IND=' On 'ClassKls(Object):Def__init__(Self,Data):Self.Data=Data@staticmethodDefCheckind():Return(IND==' On ')DefDo_reset(Self):IfSelf.Checkind():Print(' Reset done for: ',Self.Data)Defset_db (selfif self. Checkind (): self. DB = ' New db connection ' print ( DB connection made for: ' self.) ik1 = kls ( 12) ik1. Do_reset () ik1. Set_db ()
Output:
Reset done For:12
DB Connection made For:12
Here's a more comprehensive code and diagram to show the difference between the two methods
The difference between @staticmethod and @classmethod
ClassKls(Object):Def__init__(Self,Data):Self.Data=DataDefPrintd(Self):Print(Self.Data)@staticmethodDefSmethod(*Arg):Print(' Static: ',Arg)@classmethodDefCmethod(*Arg):Print(' Class: ',Arg)>>>Ik=Kls(23)>>>Ik.Printd()23>>>Ik.Smethod()Static:()>>>Ik.Cmethod()Class:(<Class‘__main__.Kls';, ')>>>Kls.Printd()TypeError:UnboundMethodPrintd()Mustbe called with kls instance as first argument (got Nothing instead) >>> kls . smethod () static: () >>> kls. Cmethod () class: (< class ' __main__. Kls
The following figure illustrates how the above code works:
Classmethod and Staticmethod in Python