Emmanuel Briot wrote: > I am participating in the development of a GPL IDE > https://libre2.adacore.com/gps/ > > It is written in Ada, but provides an extensive extensibility through > Python. > > I am not really good at python, but I was trying to implement the > singleton design pattern in C, so that for instance calling the constructor > > ed = Editor ("foo") > > would either return an existing instance of Editor currently editing > "foo", or would create a new instance. Basically, one python instance is > stored inside each of the visual windows representing an editor, and I > want to get that instance if it already exists, instead of creating a > new one each time. > > I basically would like > ed = Editor ("foo") > ed.attribute = "whatever" > print Editor ("foo").attribute > > to print "whatever" > > Has any one an example on how to do that in C, or maybe even at the > python level itself, and I will try to adapt it ?
use a factory function instead of the class, and let the function create an editor instance when needed: _editors = {} def Editor(name): try: editor = _editors[name] except KeyError: editor = _editors[name] = EditorImplementation() editor.setname(name) ... return editor if you really need singleton, click here: http://www.google.com/search?q=python+borg+pattern </F> -- http://mail.python.org/mailman/listinfo/python-list