Python中的类方法和静态
来源:互联网 发布:淘宝网店店铺名称大全 编辑:程序博客网 时间:2024/04/30 15:33
原文:http://clojure.cn/python中的类方法和静态/
转载自: http://www.tuicool.com/articles/yqmm6fu
这也不是什么难点,写一下就是感觉网上有些写得不太明白,其实不用多深入探讨,可以简单的知道区别只有如下即可:
1,普通方法须用对象调用,默认第一个参数会传入对象自身(这个“自身”习惯上用一个名为self的变量表示,其实无所谓名字,虽然贫道并不推荐改成其他)。
2,类方法可以使用对象或者类调用,类似普通方法,默认第一个参数传入类方法所在的类而不是对象(同上习惯上用cls表示)。
3,静态方法可以使用对象或者类调用,并没有默认传入的方法。
下面看个例子:
class Demo(object): def regular_method(self, arg, *args, **kwargs): print self, id(self), arg, args, kwargs @classmethod def class_method(cls, arg, *args, **kwargs): print cls, id(cls), arg, args, kwargs @staticmethod def static_method(arg, *args, **kwargs): print arg, args, kwargsdemo = Demo()demo.regular_method("a", "b", c = "c")demo.class_method("a", "b", c = "c")Demo.class_method("a", "b", c = "c")demo.static_method("a", "b", c = "c")Demo.static_method("a", "b", c = "c")
输出还是很明白的,不多说了:
<__main__.Demo object at 0x7f4f41e47c90> 139978384637072 a (‘b’,) {‘c’: ‘c’}
<class ‘__main__.Demo’> 15255136 a (‘b’,) {‘c’: ‘c’}
<class ‘__main__.Demo’> 15255136 a (‘b’,) {‘c’: ‘c’}
a (‘b’,) {‘c’: ‘c’}
a (‘b’,) {‘c’: ‘c’}
0 0
- PYTHON中的静态方法和类方法
- python中的静态方法和类方法
- PYTHON中的静态方法和类方法
- python中的静态方法和类方法
- python中的静态方法和类方法
- Python中的类方法和静态方法
- python中的类方法和静态方法
- python中的类方法和静态方法
- python中的静态方法和类方法
- Python中的类方法和静态
- python中的普通方法、类方法和静态方法
- python中的静态成员方法 和 类成员方法
- python中的静态方法和类成员方法
- python中的实例方法、静态变量,静态方法、类方法、类变量和实例变量
- python中的静态方法、类方法解读
- Python 静态方法和静态类
- python 的静态方法和静态类
- python 中的静态方法
- cordova学习笔记
- iOS- 详解文本属性Attributes
- 集成JAX-RS和JSON-P
- WCF异步调用模式
- 虚幻4随笔4 从工程开始
- Python中的类方法和静态
- Oracle Interval-Partition 解决Range分区大难题
- 如果Java 失宠于Oracle,那么未来会怎么样?
- 实习整理(十二)
- ios图片加水印或文字
- mac下安装启动Mongodb
- 运维工具大宝典之运维需求篇
- nginx 安装&配置监听端口
- 关于Bitmap的recycle()的理解