Singleton Pattern class with Cython

38 views
Skip to first unread message

Matt D

unread,
Jul 9, 2024, 12:52:17 PMJul 9
to cython-users
Hi, I'm looking to convert a group of singleton classes I have to cython, I have some arithmetic operators overloaded on them and I would like to speed that up. I am completely new to Cython and am looking to use it as opposed to going to the Python C API.

I am seeking help on how to do the singleton pattern with Cython. In pure Python I would just overwrite __new__, check that a class variable is not None (otherwise its assumed to be the singleton instance) and return it. I see Cython does not support __new__ and from some googling does not support meta type programming much. Does anyone have a small snippet to reference on how to implement the singleton pattern with Cython? Is it possible at all?

David Woods

unread,
Jul 12, 2024, 12:55:55 PM (13 days ago) Jul 12
to cython...@googlegroups.com
Hi Matt,

If you use a regular class (not a cdef class) then it'll work. However I don't know a good way of making it work with a cdef class. I think that's just the trade-off unfortunately - cdef classes are a specialised tool so can't do everything.

You might be able to have a private base cdef class and then inherit from it in a regular class and make that the singleton. However I haven't tried it so it's possible that there's issues.

Stefan Behnel

unread,
Jul 16, 2024, 4:50:13 AM (9 days ago) Jul 16
to cython...@googlegroups.com
Hi,

Matt D schrieb am 09.07.24 um 16:31:
It's not entirely clear to me what your scenario looks like, but I gather
that you have some kind of hierarchy with overloaded methods where each
instance should be a singleton of the specific class in the hierarchy?

Then my question is: why do you need a (callable) class interface for this
in the first place? Why not just expose the instances as simple module
attributes? Maybe together with their main base class (or an ABC) to
facilitate type checks on user side?

I don't think I have understood the part with the class variable, but a
class variable is really just a global variable in a namespace, so maybe
you can replace the publicly exposed classes with factory functions to keep
the interface callable, and only create the implementation classes
internally? (There's a "@cython.internal" decorator for cases like this.)

Or, as David wrote, stick to Python classes. The implementation of the
methods is then still compiled, but the classes that own them can use all
regular Python class tricks.

Stefan

Matt D

unread,
Jul 16, 2024, 9:08:15 AM (9 days ago) Jul 16
to cython...@googlegroups.com
Hi Stefan,

Let me take a step back and give a short example of what to do with cython by making a pure python class.

The singleton pattern, where a class only has 1 instance for the entirety of the program can be implemented as such (Older syntax, I took from Gang of Four python book). The class variable I am speaking of is _instance by overloading __new__ we make sure that each invocation of the class always returns _instance after the first call to Logger

I appreciate the response, and I think I may go with one of your suggestions. I most certainly can do something like a singleton without it being a singleton exactly and everything will probably be just fine, meaning the downstream uses of the so called  "singletons" will not have to change the identity checks.

class Logger(object):
    _instance = None

    def __new__(cls):
        if cls._instance is None:
            print('Creating the object')
            cls._instance = super(Logger, cls).__new__(cls)
            # Put any initialization here.
        return cls._instance

--

---
You received this message because you are subscribed to a topic in the Google Groups "cython-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cython-users/6yIfdCZOWRY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to cython-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cython-users/3e316685-2a1e-46d2-9ab4-788cabe1fe63%40behnel.de.


--
Matt Delengowski
Reply all
Reply to author
Forward
0 new messages