[Cython] Equivalent of __new__() in python

Arnaud Bergeron abergeron at gmail.com
Fri Aug 1 21:48:01 CEST 2008


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

I am looking for a way to implement the uniqueness of a class 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.

I already know how to do this in python using the __new__() method and
a WeakValueDictionary.  I am looking for a way to do the equivalent in
cython.

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.

Arnaud


More information about the Cython-dev mailing list