python cls 和 self
来源:互联网 发布:成都多益网络公司地址 编辑:程序博客网 时间:2024/05/17 21:06
在看__new__和__init__的区别的时候, 发现一个参数是cls一个参数是self, 顺便查了他们的区别
cls作为第一个参数用来表示类本身. 在类方法中用到
类方法是只与类本身有关而与实例无关的方法, 比如:
class A(object): a = 'a' @classmethod def repeat_a(cls, times=1): return cls.a * timesfoo = A()print(foo.repeat_a(2))
aa
self作为第一个参数用来表示类的实例, 在类的一般方法中用到. 类的一般方法与类的实例有关.
类中还有一种静态方法, 用@staticmethod装饰器装饰, 这种方法与类有某种关系但不需要使用到实例或者类来参与
比如:
DEBUG = Trueclass A(object): a = 'a' @staticmethod def is_debug(): return DEBUG def show_error_messages(self): if self.is_debug(): print('error_messages') else: passfoo = A()foo.show_error_messages()
0 0
- python cls 和 self
- python中self和cls
- python中self和cls的区别
- python中self和cls的区别
- python中self,cls
- 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 __str__(self)和__unicode__(self)
- Python __str__(self)和__unicode__(self)
- Python __str__(self)和__unicode__(self)
- 图片压缩处理方法
- day17:RDD案例(join、cogroup、reduceByKey、groupByKey, join cogroup
- AutoLayout
- LCD接口分类/DPI/DBI/DSI总结
- 如何解决failed to push some refs to git
- python cls 和 self
- pch 文件的创建
- App半透明指引(第一次进去app的新功能或者版本更新之后使用)
- 1038. Recover the Smallest Number (30)
- Admob Unity插件
- javascript 中的console.log有什么作用啊?是做什么的呢?谢谢大家
- day18:RDD持久化、广播、累加器
- 64位ubuntu编译32位程序
- 在oracle中sequence就是序号,每次取的时候它会自动增加。sequence与表没有关系