python中self和cls的区别
来源:互联网 发布:node 构建项目 编辑:程序博客网 时间:2024/05/21 07:40
1、self表示一个具体的实例本身。如果用了staticmethod,那么就可以无视这个self,将这个方法当成一个普通的函数使用。
2、cls表示这个类本身。
>>> class A(object): def foo1(self): print "Hello",self @staticmethod def foo2(): print "hello" @classmethod def foo3(cls): print "hello",cls>>> a = A()>>> a.foo1() #最常见的调用方式,但与下面的方式相同Hello <__main__.A object at 0x9f6abec>>>> A.foo1(a) #这里传入实例a,相当于普通方法的selfHello <__main__.A object at 0x9f6abec>>>> A.foo2() #这里,由于静态方法没有参数,故可以不传东西hello>>> A.foo3() #这里,由于是类方法,因此,它的第一个参数为类本身。hello <class '__main__.A'>>>> A #可以看到,直接输入A,与上面那种调用返回同样的信息。<class '__main__.A'>
3、whats more,类先调用__new__方法,返回该类的实例对象,这个实例对象就是__init__方法的第一个参数self,即self是__new__的返回值。
阅读全文
0 0
- python中self和cls的区别
- python中self和cls的区别
- python中self和cls
- python中self,cls
- Python中的cls与self的区别
- python cls 和 self
- python的cls,self,classmethod,staticmethod
- 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,decorator的理解
- Python 类中方法的内部变量,命名加'self.'变成 self.xxx 和不加直接 xxx 的区别
- Delphi中Self和Sender的区别
- iOS中self.和下划线的区别
- iOS中self.和下划线的区别
- iOS中self.和下划线的区别
- 记事本写java中文出现源码解决方案
- Location服务之Geocoder
- Python 中的串行执行 并发执行 同步异步
- mysql cluster数据库集群配置
- SpringMVC连接SqlServer版本问题
- python中self和cls的区别
- gfoj测试题
- 晋升为CSDN博客专家
- 2017秋招总结
- NDK_PROJECT_PATH = null
- Python如何实现两个服务器之间文件的上传
- QT之qss教程-QTabWidget
- 调整数组顺序使奇数位于偶数前面
- WebView爬坑之旅3--H5中选择本地文件