python学习

来源:互联网 发布:mac可以装bilibili吗 编辑:程序博客网 时间:2024/06/11 04:51
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 = aclass 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

0 0
原创粉丝点击