Would it be OK to declare an instance of the sub-classed class from its Base (super?) class as I do it here:
if 'classB_args' in dictArg.keys() and dictArg['classB_args']:
self['classB']=ClassA( dictArg['classB_args'] )
Are there any side effects of doing so? What should I be aware before moving forward. I have a feeling sooner or later something would go wrong... with cPickle , pickle or may be it would be a PyQt drag-and-dropt?.. if so then why would the problem arise?
class Base(dict):
def __init__(self, *args, **kwargs):
super(Base, self).__init__(*args, **kwargs)
self.setdefault('id', -1)
self.setdefault('name', None)
self.setdefault('classB', None)
if not args or len(args)==0: return
dictArg=args[0]
if 'classB_args' in dictArg.keys() and dictArg['classB_args']:
self['classB']=ClassA( dictArg['classB_args'] )
def getClassB(self):
return self['classB']
class ClassA(Base):
def __init__(self, *args, **kwargs):
if not (args or kwargs): raise Exception('you need to give me *something*!')
super(ClassA, self).__init__(*args, **kwargs)
self.setdefault('win', None)
self.setdefault('mac', None)
myDictArg= {'id':1, 'name':'MyName', 'win':'c:/windows', 'mac': '/Volumes/mac/', 'classB_args': {'id':1, 'name':'MyProject'}}
myInstance=ClassA(myDictArg)
print myInstance.getClassB()