[Cython] Equivalent of __new__() in python
Arnaud Bergeron
abergeron at gmail.com
Mon Aug 4 16:55:06 CEST 2008
2008/8/3 Stefan Behnel <stefan_ml at behnel.de>:
> 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.
Yes, a singleton.
> 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:
Yes that's what I mean.
> 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.
I don't need subclassing right now so I'll probably go with the
factory approach.
If I have some free time, I'll try to do a patch against Cython
implementing more or less what I am searching for.
Thanks for your answers.
Arnaud
> Stefan
> _______________________________________________
> Cython-dev mailing list
> Cython-dev at codespeak.net
> http://codespeak.net/mailman/listinfo/cython-dev
More information about the Cython-dev
mailing list