Singleton and C extensions

E

Emmanuel Briot

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 ?

Thanks in advance

Emmanuel
 
F

Fredrik Lundh

Emmanuel said:
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>
 
S

Scott David Daniels

Emmanuel said:
I am participating in the development of a GPL IDE
https://libre2.adacore.com/gps/
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")

Fredrik's advice is probably better, but if you must:

class Editor(object):
table = {}
def __new__(klass, name):
try:
return klass.table[name]
except KeyError:
klass.table[name] = super(Singled, klass).__new__(klass)
return klass.table[name]

--Scott David Daniels
(e-mail address removed)
 
N

Neal Norwitz

Emmanuel said:
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.

Hmm, if there can be more than one Editor I wouldn't call it a
singleton.
But this should do what you want:

class Editor(object):
_cache = {}
def __init__(self, arg):
self._cache[arg] = self
def __new__(cls, arg):
if arg in cls._cache:
return cls._cache[arg]
return object.__new__(cls, arg)
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 ?

C is a *lot* more work and tricky too.

hth,
n
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,271
Messages
2,571,357
Members
48,042
Latest member
DoraMcBrie

Latest Threads

Top