While looking for an elegant implementation of the singleton design pattern I came across the decorator as described in <a href="http://www.python.org/dev/peps/pep-0318/">PEP318</a>.<br>Unfortunately, the following does not work, because decorators only work on functions or methods, but not on classes.
<br><br><pre style="font-family: courier new,monospace;" class="literal-block">def singleton(cls):<br> instances = {}<br> def getinstance():<br> if cls not in instances:<br> instances[cls] = cls()<br>
return instances[cls]<br> return getinstance<br><br>@singleton<br>class MyClass:<br> ...</pre><br>Am I missing something here? What is the preferred pythonic way of implementing singleton elegantly?<br><br>Thanks for your help
<br>André<br><br>