python 类方法(classmethod)小实验代码
来源:互联网 发布:留学银行卡 知乎 编辑:程序博客网 时间:2024/06/05 06:50
class c(object): foralll = 8 def __init__(self, i, forall): self.i = i self.test = 0 self.foralll = forall print self.i print self.foralll def do_print(self): print "OK" @classmethod def do_print_again(clss): print "OKOK" @classmethod def smthd(clss, i, forall): clss.foralll = forall print clss.foralll clss.do_print_againcc = c(0,0)cc.do_print()c.smthd(2, 3)
result:
0 0 OK OKOK 3
python里面有两个概念,类和实例,凡是def XXX(self)的方法,以及self.XXX的变量都是只能由实例调用的
实例就是指代码中的"cc", 是对类实例化后的结果。
而带有@classmethod修饰符的方法,只能调用类中同样由@classmethod修饰符修饰的方法,
和在init方法之前声明的那些变量(类变量?)
相比较@staticmethod 就不能调用类中的任何值和方法了
(二) 一个对比测试
class c(object): foralll = 8 def __init__(self): print self.foralll @classmethod def smthd(clss, i, forall): clss.foralll = forall print clss.foralllcc = c()print cc.foralllc.smthd(2, 3)print cc.foralll
result:
8833
class c(object): foralll = 8 def __init__(self): self.foralll = 0 # differential print self.foralll @classmethod def smthd(clss, i, forall): clss.foralll = forall print clss.foralllcc = c()print cc.foralllc.smthd(2, 3)print cc.foralll
result:
0030
- python 类方法(classmethod)小实验代码
- python classmethod类方法
- Python 类方法classmethod 示例
- python-静态方法staticmethod、类方法classmethod、属性方法property
- python 静态方法staticmethod和类方法classmethod
- classmethod类方法
- python 类中@staticmethod,@classmethod和实例方法
- 我所理解的类方法(classmethod)(关键词:类方法/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内置函数
- MyEclipse安装Spket插件
- 文件描述符和文件指针的区别
- 采取个hook技巧对writefile函数进行阻碍(2)
- ubuntu编译安装gvim
- ORACLE 一条sql语句生成前60年的年份
- python 类方法(classmethod)小实验代码
- shell脚本学习指南-01
- iPhone中的剪切技巧
- iPhone下主要的目录的意义
- 如何用MsiZap.exe 卸载软件
- 两点确定直线方程
- 默然日记20120521
- 使用XIB自定义一个UIView,然后将这个view添加到controller的view
- CUDA编程中遇到的警告信息:Warning: Cannot tell what pointer points to, assuming global memory space