python classmethod类方法
来源:互联网 发布:水质检测笔的参考数据 编辑:程序博客网 时间:2024/06/05 19:54
python classmethod类方法的要点主要有3个:
1 在python中.类方法 @classmethod 是一个函数修饰符,它表示接下来的是一个类方法,而对于平常我们见到的则叫做实例方法。 类方法的第一个参数cls,而实例方法的第一个参数是self,表示该类的一个实例。
2 普通对象方法至少需要一个self参数,代表类对象实例
3 类方法有类变量cls传入,从而可以用cls做一些相关的处理。并且有子类继承时,调用该类方法时,传入的类变量cls是子类,而非父类。 对于类方法,可以通过类来调用,就像C.f(),有点类似C++中的静态方法, 也可以通过类的一个实例来调用,就像C().f(),这里C(),写成这样之后它就是类的一个实例了。
class info(object):
@classmethod
def sayclassmethod(cls):
print 'say %s' % cls
def saymethod(self):
print 'say %s' % self
test = info()
test.saymethod()##实例调用方法
test.sayclassmethod()##实例调用类方法
info.saymethod(test)##类调用实例方法
info.sayclassmethod()##类调用类方法
>>>
say <__main__.info object at 0x0000000002CF6278>
say <class '__main__.info'>
say <__main__.info object at 0x0000000002CF6278>
say <class '__main__.info'>
- python classmethod类方法
- Python 类方法classmethod 示例
- python-静态方法staticmethod、类方法classmethod、属性方法property
- python 静态方法staticmethod和类方法classmethod
- classmethod类方法
- python 类方法(classmethod)小实验代码
- python 类中@staticmethod,@classmethod和实例方法
- Python 实例方法、@staticmethod和@classmethod
- Python 实例方法、@staticmethod和@classmethod
- Python 实例方法、@staticmethod和@classmethod
- python的classmethod和staticmethod方法
- Python 实例方法、@staticmethod和@classmethod
- python classmethod
- Python @classmethod
- PYTHON @classmethod
- 静态方法和类方法, staticmethod & classmethod
- python之静态和类方法:staticmethod和classmethod内置函数
- Python中staticmethod方法和classmethod方法区别
- JavaScript(2)----基本数据类型
- TIKA架构
- php的IP转换成整型函数ip2long()易出现负数
- 透过ssh使用VNC(VNC over SSH)
- linux压缩与解压缩
- python classmethod类方法
- 集合
- sdcc man阅读笔记(五)——mem文件与map文件
- 代码风格配置格式化代码
- 机房合作(三):We are Team,We are Family
- rvm管理rails不同版本
- 使用Swift作为Glance后端存储
- strcpy和memcpy函数的实现
- Javamail中的常见中文乱码问题与解决办法(综合)