-1

I have two singleton subclasses A and B, which data needs to be maintained separately. When one subclass A make use of superclass, some of the informations will be stored in list and dict in superclass. But when I try to invoke superclass from another subclass B the informations that are stored by subclass A causing issues.

Is it possible to have seperate instance of superclass for both A and B subclasses.

(I used self for superclass members)

Kuba hasn't forgotten Monica
  • 88,505
  • 13
  • 129
  • 275

3 Answers3

1

Data members of base classes are not shared in Python

class Base(object):
    def __init__(self, x):
        self.x = x

class Derived1(Base):
    def __init__(self, x, y):
        Base.__init__(self, x)
        self.y = y

class Derived2(Base):
    def __init__(self, x, z):
        Base.__init__(self, x)
        self.z = z

d1 = Derived1(10, 20)
d2 = Derived2(30, 40)
print d1.x   # Will show 10
print d2.x   # Will show 30

The problem you're observing is probably related to something else

6502
  • 104,192
  • 14
  • 145
  • 251
  • 1
    The OP's problem is likely because both subclasses are derived from the same singleton base class which probably -- who knows, there's no code shown -- only allows one instance of itself to ever be created. – martineau Sep 13 '13 at 18:01
1

I'm not sure I completely understand your problem, partly because you haven't included any code in your question. So, based solely on my interpretation of the description you wrote, I think the following will address the issue of the two singleton subclasses interfering with each other.

A generic Singleton metaclass is used to define the base singleton class. The metaclass ensures that a separate single instance is created for each singleton class instance of itself -- i.e. your MySingletonBase in this case -- which is exactly what I think you want. The two subclasses derived from it, A and B, will inherit this metaclass from the baseclass and thereby will have independent instances of their singleton superclass.

This code below is based on one of my answers to the question:
     How to initialize Singleton-derived object once?

# singleton metaclass
class Singleton(type):
    _instances = {}
    def __call__(cls, *args, **kwargs):
        if cls not in cls._instances:
            cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
        return cls._instances[cls]

class MySingletonBase(object):
    __metaclass__ = Singleton
    def __init__(self):
        self.values_list = []
    def test_method(self, value):
        self.values_list.append(value)
    def total(self):
        return sum(self.values_list)

class A(MySingletonBase): pass

class B(MySingletonBase): pass

a1 = A()
b1 = B()
a2 = A()

a1.test_method(42)
a2.test_method(13)
print '{}, {}'.format(a1.values_list, a1.total())  # [42, 13], 55
print '{}, {}'.format(a2.values_list, a2.total())  # [42, 13], 55
print '{}, {}'.format(b1.values_list, b1.total())  # [], 0

The output illustrates that instances of subclass A are indeed singletons, and are separate from any instances created of subclass B, yet they are both inheriting MySingletonBase's methods.

Community
  • 1
  • 1
martineau
  • 99,260
  • 22
  • 139
  • 249
0

thanks for your response,

The mistake that I made was I declared base class members list1 in wrong place,

class BaseClass(metaclass=Singleton):
    list1 = []
    def __init__(self, a, b, c):
        self.list1.extend([a,b,c])

    def printList(self):
        print(self.list1)

class SubClass1(BaseClass):
    def __init__(self):
        super(SubClass1, self).__init__('a', 'b', 'c')

class SubClass2(BaseClass):
    def __init__(self):
        super(SubClass2, self).__init__('x','y','z')

if '__main__' == __name__:

    s1 = SubClass1()
    s2 = SubClass2()
    s1.printList()
    s2.printList()

The problem has been solved, once after I declared list1 inside init method

class BaseClass(metaclass=Singleton):
    def __init__(self, a, b, c):
        self.list1 = []
        self.list1.extend([a,b,c])
  • Showing some code would have helped answer your question, as would tagging or at least mentioning that you were using Python 3... – martineau Sep 16 '13 at 10:16