Hi,

Arnaud Bergeron wrote:
> Sorry if this is the wrong list to ask questions like that, I couldn't
> find any other list for cython.

cython-dev is our general purpose list, so it's fine to ask here.


> I am looking for a way to implement the uniqueness of a class in
> Cython.

You mean a singleton. A class is always unique, but it's instances are not
necessarily.

There are many recipes for implementing singletons in Python, many of which
also work in Cython.


> I have a class say:
> 
> cdef class Unique(object):
>     cdef val
> 
>     def __init__(self, val):
>         self.val = val
> 
> And I want to have only one instance at a time with a single value of val.

If you meant one instance for each distinct value of val, then a factory might
work:

  cdef class _Unique(object):
      cdef object __weakref__
      ...

  def Unique(val):
      # do your usual WeakValueDictionary handling here.
      return the_right_instance


> And if this requires monkey-patching the tp_new slot of the class,
> perhaps some pointers as to what should the replacement method should
> at least do in order not to screw the rest of the system.

I'd avoid that for now, unless you really require subclassing support etc.

Stefan
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to