Python中的self,cls参数
来源:互联网 发布:网络支付概念股 编辑:程序博客网 时间:2024/05/04 22:18
python类里会出现这三个单词,self和cls都可以用别的单词代替,类的方法有三种,
一是通过def定义的 普通的一般的,需要至少传递一个参数,一般用self,这样的方法必须通过一个类的实例去访问,类似于c++中通过对象去访问;
二是在def前面加上@classmethod,这种类方法的一个特点就是可以通过类名去调用,但是也必须传递一个参数,一般用cls表示class,表示可以通过类直接调用;
三是在def前面加上@staticmethod,这种类方法是静态的类方法,类似于c++的静态函数,他的一个特点是参数可以为空,同样支持类名和对象两种调用方式;
代码:
- class A:
- member = "this is a test."
- def __init__(self):
- pass
- @classmethod
- def Print1(cls):
- print "print 1: ", cls.member
- def Print2(self):
- print "print 2: ", self.member
- @classmethod
- def Print3(paraTest):
- print "print 3: ", paraTest.member
- @staticmethod
- def print4():
- print "hello"
- a = A()
- A.Print1()
- a.Print1()
- #A.Print2()
- a.Print2()
- A.Print3()
- a.Print3()
- A.print4()
0 0
- Python中的self,cls参数
- Python中的cls与self的区别
- cls, self 参数
- python cls 和 self
- python中self,cls
- 对于python中的self,cls,decorator的理解
- python中的self参数
- python中self和cls
- python中的默认参数self
- python的cls,self,classmethod,staticmethod
- python的cls,self,classmethod,staticmethod
- python的cls,self,classmethod,staticmethod
- python的cls,self,classmethod,staticmethod
- python中self和cls的区别
- python中self和cls的区别
- python的cls,self,classmethod,staticmethod
- cls 参数
- 【python】Python中的self
- 【转载】软件开发模型对比
- 写给那些让我糊里糊涂的HTTP、TCP、UDP、Socket
- spring下的几种注解
- UIVisualEffectView 毛玻璃效果的应用
- 天天猜拳
- Python中的self,cls参数
- H5页面性能优化
- Zookeeper的Java客户端
- HDU 1143 Tri Tiling
- Linux下HTTP Server
- tomact登录问题
- poj 2778 AC自动机+矩阵快速幂
- webrtc 编译汇编文件时错误
- MYSQL 存储过程的事务