您的位置:首页 > 编程语言 > Python开发

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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: