使Daniel Brodie&ŧ039;的单例配方使用关键字参数
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使Daniel Brodie&ŧ039;的单例配方使用关键字参数相关的知识,希望对你有一定的参考价值。
The ASPN cookbook has many recipes for singletons in Python. So far, this onehttp://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/412551
has been my favourite, because it is so simple and concise. However, I just ran into a brick wall when I tried to use it with a class that can be initialized with keyword arguments. This is my first draft for a solution to this problem. It's quick and dirty; please give it a glance and leave a comment if you find a problem.
class Singleton(type): def __init__(self, *args, **kwds): type.__init__(self, *args, **kwds) self._instances = {} def __call__(self, *args, **kwds): sig = args + tuple(sorted(kwds.items())) if not sig in self._instances: self._instances[sig] = type.__call__(self, *args, **kwds) return self._instances[sig]
以上是关于使Daniel Brodie&ŧ039;的单例配方使用关键字参数的主要内容,如果未能解决你的问题,请参考以下文章
检查是否安装了Apache&ŧ039;的modŧu rewrite。
使用TextMate&ŧ039;的tmŧdialog命令创建用户定义的菜单