标签:http src sel blog python out highlight stat eth
实例方法:
class A: data = 0 def printd(self): print(self.data)
type(A) #out:type
A.__dict__ #out:
a = A() #out:{}
类方法:@classmethod
class A: data = 0 def printd(self): print(self.data) def getdata(): A.data = 1 print(A.data)
class A: data = 0 def printd(self): print(self.data) def getdata(): A.data = 1 print(A.data)
a = A()
a.getdata() #out:
目的:写一个跟类交互,不跟实例交互的方法
class A: data = 0 def printd(self): print(self.data) @classmethod def getdata(cls): cls.data = 1 print(cls.data)
静态方法:
IND = ‘NO‘ def checkind(): return IND == ‘NO‘ class Kls: def __init__(self,data): self.data = data def do_reset(self): if checkind(): print(‘set done for‘,self.data) def set_db(self): if checkind(): self.db = ‘new db connection‘ print(‘DB connection made for:‘,self.data)
IND = ‘NO‘ class Kls: def __init__(self,data): self.data = data def checkind(): return IND == ‘NO‘ def do_reset(self): if checkind(): print(‘set done for‘,self.data) def set_db(self): if checkind(): self.db = ‘new db connection‘ print(‘DB connection made for:‘,self.data)
IND = ‘NO‘ class Kls: def __init__(self,data): self.data = data @staticmethod def checkind(): return IND == ‘NO‘ def do_reset(self): if checkind(): print(‘set done for‘,self.data) def set_db(self): if checkind(): self.db = ‘new db connection‘ print(‘DB connection made for:‘,self.data)
TODO:调用静态方法加self与不加self
标签:http src sel blog python out highlight stat eth
原文地址:http://www.cnblogs.com/jons/p/7118832.html