python @classmethon @staticmethod 的使用及区别
来源:互联网 发布:java方法内部类 编辑:程序博客网 时间:2024/04/30 05:24
转自:http://www.zhihu.com/question/20021164
举个例子,一个基本的实例方法就向下面这个:
class Kls(object): def __init__(self, data): self.data = data def printd(self): print(self.data)ik1 = Kls('arun')ik2 = Kls('seema')ik1.printd()ik2.printd()
这会给出如下的输出:
seema
然后看一下代码和示例图片:
- 1,2参数传递给方法.
- 3 self参数指向当前实例自身.
- 4 我们不需要传递实例自身给方法,Python解释器自己会做这些操作的.
def get_no_of_instances(cls_obj): return cls_obj.no_instclass Kls(object): no_inst = 0 def __init__(self): Kls.no_inst = Kls.no_inst + 1ik1 = Kls()ik2 = Kls()print(get_no_of_instances(Kls))
输出:
2
@classmethod
我们要写一个只在类中运行而不在实例中运行的方法. 如果我们想让方法不在实例中运行,可以这么做:
def iget_no_of_instance(ins_obj): return ins_obj.__class__.no_instclass Kls(object): no_inst = 0 def __init__(self): Kls.no_inst = Kls.no_inst + 1ik1 = Kls()ik2 = Kls()print iget_no_of_instance(ik1)
输出
2
在Python2.2以后可以使用@classmethod装饰器来创建类方法.
class Kls(object): no_inst = 0 def __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.get_no_of_instance()
输出:
2
2
这样的好处是: 不管这个方式是从实例调用还是从类调用,它都用第一个参数把类传递过来.
@staticmethod
经常有一些跟类有关系的功能但在运行时又不需要实例和类参与的情况下需要用到静态方法. 比如更改环境变量或者修改其他类的属性等能用到静态方法. 这种情况可以直接用函数解决, 但这样同样会扩散类内部的代码,造成维护困难.
比如这样:
IND = 'ON'def checkind(): return (IND == 'ON')class Kls(object): def __init__(self,data): self.data = datadef do_reset(self): if checkind(): 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(12)ik1.do_reset()ik1.set_db()
输出:
Reset done for: 12
DB connection made for: 12
如果使用@staticmethod就能把相关的代码放到对应的位置了.
IND = 'ON'class Kls(object): def __init__(self, data): self.data = data @staticmethod def checkind(): return (IND == 'ON') def do_reset(self): if self.checkind(): print('Reset done for:', self.data) def set_db(self): if self.checkind(): self.db = 'New db connection' print('DB connection made for: ', self.data)ik1 = Kls(12)ik1.do_reset()ik1.set_db()
输出:
Reset done for: 12
DB connection made for: 12
下面这个更全面的代码和图示来展示这两种方法的不同
@staticmethod 和 @classmethod的不同
class Kls(object): def __init__(self, data): self.data = data def printd(self): print(self.data) @staticmethod def smethod(*arg): print('Static:', arg) @classmethod def cmethod(*arg): print('Class:', arg) >>> ik = Kls(23)>>> ik.printd()23>>> ik.smethod()Static: ()>>> ik.cmethod()Class: (<class '__main__.Kls'>,)>>> Kls.printd()TypeError: unbound method printd() must be called with Kls instance as first argument (got nothing instead)>>> Kls.smethod()Static: ()>>> Kls.cmethod()Class: (<class '__main__.Kls'>,)
下面这个图解释了以上代码是怎么运行的:
0 0
- python @classmethon @staticmethod 的使用及区别
- python中staticmethod classmethod及普通函数的区别
- python中staticmethod classmethod及普通函数的区别
- python中staticmethod classmethod及普通函数的区别
- 【Python】Staticmethod和classmethod的区别
- python中staticmethod和classmethod的区别
- Python的classmethod和staticmethod区别
- python中classmethod和staticmethod的区别
- python中staticmethod和classmethod的区别
- Python: classmethod & staticmethod 区别
- Python: classmethod & staticmethod 区别
- Python: classmethod & staticmethod 区别
- python @staticmethod @classmethod 区别
- python 装饰器@staticmethod和@classmethod区别和使用
- python的staticmethod、classmethod
- python 的@classmethod &&@staticmethod
- Python @classmethod和@staticmethod区别
- python中@classmethod @staticmethod区别
- java随机加法游戏
- 【Github教程】史上最全github使用方法:github入门到精通
- Java深度历险(六)——Java注解
- gcc与g++的区别
- unity3d利用sharesdk分享内容至微信、新浪微博、qq空间等国内主流社交平台(ios篇)
- python @classmethon @staticmethod 的使用及区别
- PHP程序员最常犯的11个MySQL错误
- java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
- Adobe Acrobat使用高亮文本标记,但是文字却被遮住了
- Date,Calendar实例
- 盘点PHP编程常见失误
- 关于委托的一点理解。
- 气泡型弹出菜单效果
- 根据csproj文件查看项目的版本信息