python中self,cls
来源:互联网 发布:种植牙 知乎 编辑:程序博客网 时间:2024/05/22 14:56
python中self,cls
普通的方法,第一个参数需要是self,它表示一个具体的实例本身。
如果用了staticmethod,那么就可以无视这个self,而将这个方法当成一个普通的函数使用。
而对于classmethod,它的第一个参数不是self,是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,相当于普通方法的self
Hello <__main__.A object at 0x9f6abec>
>>> A.foo2() #这里,由于静态方法没有参数,故可以不传东西
hello
>>> A.foo3() #这里,由于是类方法,因此,它的第一个参数为类本身。
hello <class '__main__.A'>
>>> A #可以看到,直接输入A,与上面那种调用返回同样的信息。
<class '__main__.A'>
阅读全文
0 0
- python中self,cls
- python中self和cls
- python中self和cls的区别
- python中self和cls的区别
- python cls 和 self
- Python中的self,cls参数
- python的cls,self,classmethod,staticmethod
- python的cls,self,classmethod,staticmethod
- python的cls,self,classmethod,staticmethod
- python的cls,self,classmethod,staticmethod
- Python中的cls与self的区别
- python的cls,self,classmethod,staticmethod
- 对于python中的self,cls,decorator的理解
- cls, self 参数
- 关于静态方法,self,cls
- python中self意义
- Python中self详解
- 关于 python cls 笔记
- Unity Shader 学习笔记(3)Unity Shader模板、结构、形式
- #Thinking in Java阅读笔记# 第十一章 持有对象
- ExpressRoute 先决条件和清单
- OSI七层与TCP/IP五层网络架构详解
- jquery 合并两个 json 对象
- python中self,cls
- Go语言发展浅谈
- nginx服务一---nginx部署安装记录
- Note of deep JVM(1)
- MyBatis的JAVA类中,@Select中的sql根据条件执行不同的查询条件
- Android Activity的四大启动模式
- Android 6.0动态申请权限
- 集成百度SDK遇到的bug
- Intellij Idea 2017创建web项目及tomcat部署实战