Let's say I have the following class :
class Context:
element_list = []
@classmethod
def add(cls, element):
cls.element_list.append(element)
@classmethod
def remove(cls, element):
cls.element_list.remove(cls.element_list.index(element))
This class is near of a Singleton class. The objective is to update element_list
attribute anywhere in my program without passing a Context
instance as parameter of my functions.
PyCharm signals me that I should define an __init__
method. But I do not want to create two different instances of this class. I was thinking about creating a dummy __init__
method like this :
def __init__(self):
raise NotImplementedError("This class should not be initialized")
The question(s) is (are) : should I define an __init__
method ? if yes how ? instead of using classmethod
implementation should I use a singleton class (see: Is there a simple, elegant way to define singletons?)