一段管理实例的代码

来源:互联网 发布:日本留学费用 知乎 编辑:程序博客网 时间:2024/05/24 04:16

logging中很好一段代码,在返回实例前先通过类的参数检查是否存在,存在就返回已存在的实例,不存在就实例化

    def getLogger(self, name):        """        Get a logger with the specified name (channel name), creating it        if it doesn't yet exist. This name is a dot-separated hierarchical        name, such as "a", "a.b", "a.b.c" or similar.        If a PlaceHolder existed for the specified name [i.e. the logger        didn't exist but a child of it did], replace it with the created        logger and fix up the parent/child references which pointed to the        placeholder to now point to the logger.        """        rv = None        if not isinstance(name, basestring):            raise TypeError('A logger name must be string or Unicode')        if isinstance(name, unicode):            name = name.encode('utf-8')        _acquireLock()        try:            if name in self.loggerDict:                rv = self.loggerDict[name]                if isinstance(rv, PlaceHolder):                    ph = rv                    rv = (self.loggerClass or _loggerClass)(name)                    rv.manager = self                    self.loggerDict[name] = rv                    self._fixupChildren(ph, rv)                    self._fixupParents(rv)            else:                rv = (self.loggerClass or _loggerClass)(name)                rv.manager = self                self.loggerDict[name] = rv                self._fixupParents(rv)        finally:            _releaseLock()        return rv