python学习
2016-12-18 18:52
295 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/mathorlee/article/details/53729046
class TestMethod(object): @classmethod def cm(cls): print 'class method' print cls print cls.__name__ @staticmethod def sm(): print 'static method' class Test1(object): def __init__(self, a): self.a = a class Test2(TestMethod, Test1): def __init__(self, a): TestMethod.__init__(self) Test1.__init__(self, a) class A(object): ''' class A's doc. ''' def __init__(self): print 'init' def __new__(cls, *args, **kwargs): print 'new, %s' % cls.__name__ return object.__new__(cls, *args, **kwargs) class Singleton(object): instance = None def __new__(cls, *args, **kwargs): if not Singleton.instance: Singleton.instance = object.__new__(cls, *args, **kwargs) return Singleton.instance
关于静态方法和类方法,没太大区别。类方法的第一个参数是类对象。二者皆可以被类和实例调用
new, init方法是在object里被定义的。当实例化时,先调用new生成实例,再把该实例传给init函数,调用init。重载new可实现单例模式。
好东东。
skill_issues
http://git.oschina.net/duoduo3_69/projects
相关文章推荐
- python 学习记录(1)—对内建str的处理
- python学习(一)
- 学习了python的基本语法之后
- python学习_实现一个考拉咨猜想
- Python学习20:利用函数来打印文件内容
- Python学习感悟
- python 学习札记(2)
- python学习笔记-Day02 -第二部分
- Python学习笔记二:列表、字典、元组、集合 简介
- 请不要重复犯我在学习Python和Linux系统上的错误
- python cookbook第三版学习笔记 一
- python学习笔记-升级pip报错:Could not find a version that satisfies the requirement pip==19.1.1
- 学习笔记:python
- 利用Python学习RabbitMQ消息队列
- python标准模块学习13_______urllib2模块
- Python学习记录(二)
- 中文分词jieba python 学习
- Python学习笔记——迭代器和生成器
- Python学习第一天
- python基础教程_学习笔记6:字典