Skip to content

Latest commit

 

History

History
95 lines (76 loc) · 2.51 KB

class4_thorough.md

File metadata and controls

95 lines (76 loc) · 2.51 KB

python 类深入

特殊方法

__new__() 方法:

在python内部,真正的初始化函数时__new__()方法,它在__init__()方法之前被调用,它是一个类方法,在创建对象时调用。 而__init__()方法是在创建完对象后调用,对当前对象的实例做一些一些初始化,无返回值。 如果重写了__new__()而在__new__()里面没有调用__init__()或者没有返回实例,那么__init__()将不起作用。

使用
  • 使用__new__()方法设计单例模式
import threading
lock = threading.Lock()


class Singleton(object):
    __instance = None

    def __init__(self):
        pass

    def __new__(cls, *args):
        if not Singleton.__instance:
            # set lock keep thread safe
            try:
                lock.acquire()
                if not Singleton.__instance:
                    Singleton.__instance = object.__new__(cls, *args)
            except Exception, e:
                print 'Singleton: init error : %s' % e
            finally:
                lock.release()
        return Singleton.__instance


### TEST
s1 = Singleton()
s2 = Singleton()
s1.dicts = {'name': 'tom'}

print id(s2) == id(s1), s2.dicts

__setattr__() 方法:

python可用动态给对象添加属性,禁止添加属性需要重写该方法

 def __setattr__(self, key, value):
        pass

__dict__() 方法:

使用__dict__()方法用于返回对象的属性字典,python重写__setattr__()方法禁止对象添加属性, 但是可以通过 obj.__dict__['index']= 11添加属性,重写__dict__()方法可以禁用此方法

  def __dict__(self):
        pass
构造全局字典
import threading

lock = threading.Lock()


class ApplicationDICT(object):
    __instance = None
    __maps = {}

    def __new__(cls, *args):
        if not ApplicationDICT.__instance:
            # set lock keep thread safe
            try:
                lock.acquire()
                if not ApplicationDICT.__instance:
                    ApplicationDICT.__instance = object.__new__(cls, *args)
            except Exception, e:
                print 'Singleton: init error : %s' % e
            finally:
                lock.release()
        return ApplicationDICT.__instance

    @property
    def maps(self):
        return self.__maps

    def set_maps(self, k, v):
        assert k and v
        self.__maps[k] = v

    def __setattr__(self, key, value):
        pass

    def __dict__(self):
        pass