Example method:
Class A: data = 0 def printd (self): print (Self.data)
Type (A) #out: Type
a.__dict__ #out:
A = A () #out: {}
Class method: @classmethod
class a:data = def Printd (self):
print
(self.data) def GetData (): A.data = 1 print (a.data)
class A: = 0 def printd (self): print(self.data) def GetData (): = 1 print(a.data)
A = A ()
A.getdata () #out:
Purpose: To write a method that interacts with the class and does not interact with the instance
class A: = 0 def printd (self): print(self.data) @classmethod def GetData (CLS): = 1 print(cls.data)
static method:
IND ='NO'defcheckind ():returnIND = ='NO' classKls:def __init__(self,data): Self.data=DatadefDo_reset (self):ifcheckind ():Print('set done for', Self.data)defset_db (self):ifcheckind (): Self.db='New DB connection' Print('DB Connection made for:', Self.data)
IND ='NO' classKls:def __init__(self,data): Self.data=Datadefcheckind ():returnIND = ='NO' defDo_reset (self):ifcheckind ():Print('set done for', Self.data)defset_db (self):ifcheckind (): Self.db='New DB connection' Print('DB Connection made for:', Self.data)
IND ='NO' classKls:def __init__(self,data): Self.data=Data @staticmethoddefcheckind ():returnIND = ='NO' defDo_reset (self):ifcheckind ():Print('set done for', Self.data)defset_db (self):ifcheckind (): Self.db='New DB connection' Print('DB Connection made for:', Self.data)
TODO: Call the static method plus self and do not add self
Python static methods, instance methods, class methods